android adb 技术周总结 2027.06.24~06.30(Python并发执行&shell并发执行) - 以下是一些示例用法:

📅 2026-12-01 👤 拼多多助力营销技巧 📂 拼多多助力平台 👁️ 206 次阅读

文章目录

一、 06.26 周三1.1)问题01:怎么在mysql的命令行中查询出来 python能使用的元祖结果集1.2)问题02:python中 set()是什么,怎么使用

二、06.27 周四2.1)问题01:shell 并发执行2.2)问题02:python 并发执行一段程序2.2.1)遇到的问题及解决:

2.3)问题03:python中运行 shell命令

三、06.29 周六3.1)问题01:Python运行报错处理重现错误修复方法正确使用示例

3.2)问题02:python3 list转字符串假设有一个数字列表先将数字列表中的元素转换为字符串,然后用join方法连接,用逗号分隔

一、 06.26 周三

1.1)问题01:怎么在mysql的命令行中查询出来 python能使用的元祖结果集

在 MySQL 的命令行中,您可以执行查询并使用SELECT语句来检索数据。如果您想要获取一个 Python 可以使用的元组结果集,可以使用 MySQL 命令行客户端的--skip-column-names选项来禁用列名,并且使用\G选项来以垂直方式显示结果。

下面是一个示例,假设您有一个名为users的表,其中包含id和name列:

SELECT id, name FROM users;

如果您想要以元组形式显示结果集,可以这样做:

mysql -u your_username -p your_database -e "SELECT id, name FROM users;" --skip-column-names | sed 's/\t/,/g' | sed 's/^/(/' | sed 's/$/)/' | sed 's/\n/,/g' | sed 's/,,/),(/g'

这将以元组形式显示结果集,例如(1, 'Alice'),(2, 'Bob'),(3, 'Charlie')。您可以将这些结果集复制到 Python 代码中并将其用作元组。

1.2)问题02:python中 set()是什么,怎么使用

在Python中,set()是一个内置函数,用于创建集合对象。集合是一种无序、不重复的数据结构。你可以使用set()函数来创建空集合或从其他可迭代对象(如列表、元组等)创建集合。

以下是一些示例用法:

创建空集合:

my_set = set()

从列表创建集合:

my_list = [1, 2, 3, 1, 2]

my_set = set(my_list)

print(my_set) # 输出: {1, 2, 3}

添加元素到集合:

my_set.add(4)

print(my_set) # 输出: {1, 2, 3, 4}

从字符串创建集合:

my_string = "hello"

my_set = set(my_string)

print(my_set) # 输出: {'o', 'h', 'e', 'l'}

通过set()函数,你可以方便地操作集合对象,进行集合运算(如并集、交集、差集等)。

二、06.27 周四 拼多多帮助力入口

2.1)问题01:shell 并发执行

在Shell脚本中,可以使用&符号来实现并发执行多个命令或任务。这样可以让多个命令同时运行,而不必等待一个命令执行完毕后再执行下一个命令。下面是一个简单的示例:

#!/bin/bash

# Command 1 runs in the background

command1 &

# Command 2 runs in the background

command2 &

# Wait for all background processes to finish

wait

在这个示例中,command1 和 command2 是两个要并发执行的命令。通过在命令末尾加上&符号,可以让它们在后台同时运行。wait命令用于等待所有后台进程执行完毕。

2.2)问题02:python 并发执行一段程序

在 Python 中,可以使用多线程或多进程来实现并发执行一段程序。以下是使用 concurrent.futures 模块中的 ThreadPoolExecutor 和 ProcessPoolExecutor 实现并发执行的示例:

使用 ThreadPoolExecutor 实现多线程并发执行:

import concurrent.futures

def your_function():

# Your code here

print("Executing your function")

# Create a ThreadPoolExecutor with 5 threads

with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:

# Submit your function to the executor

future = executor.submit(your_function)

# Get the result (this will block until the function is done)

result = future.result()

使用 ProcessPoolExecutor 实现多进程并发执行:

import concurrent.futures

def your_function():

# Your code here

print("Executing your function")

# Create a ProcessPoolExecutor with 5 processes

with concurrent.futures.ProcessPoolExecutor(max_workers=5) as executor:

# Submit your function to the executor

future = executor.submit(your_function)

# Get the result (this will block until the function is done)

result = future.result()

这些示例展示了如何使用 ThreadPoolExecutor 和 ProcessPoolExecutor 来实现并发执行一段程序。根据需要选择线程或进程执行模式。

2.2.1)遇到的问题及解决:

ModuleNotFoundError: No module named ‘concurrent.futures’; ‘concurrent’ is not a package 这错误通常是由于在Python 2中引入的concurrent.futures模块在Python 3中被删除导致的。

确保你正在使用Python 3,并尝试以下修复方法:

使用Python 3: 确保你的Python环境是Python 3。 尝试使用ThreadPoolExecutor和ProcessPoolExecutor的替代方法:如果concurrent.futures不可用,你可以尝试使用threading模块来实现多线程,并使用multiprocessing模块来实现多进程。这两个模块是Python标准库的一部分,应该可以在任何Python环境中使用。

下面是一个使用threading模块的示例:

import threading

def your_function():

print("Executing your function")

# 创建并启动5个线程

threads = []

for _ in range(5):

thread = threading.Thread(target=your_function)

threads.append(thread)

thread.start()

# 等待所有线程完成

for thread in threads:

thread.join()

这种方法可以在不使用concurrent.futures的情况下实现并发执行。

2.3)问题03:python中运行 shell命令

在Python中,你可以使用subprocess模块来运行Shell命令 当你需要将命令和参数组合成一个字符串时,你可以使用shell=True参数来运行Shell命令。这样可以让你直接将命令和参数组合成一个字符串作为参数传递给subprocess.run()函数。下面是一个示例:

import subprocess

# 组合命令和参数成一个字符串

command = "ls -l"

# 运行Shell命令

result = subprocess.run(command, shell=True, stdout=subprocess.PIPE, text=True)

# 打印命令的输出

print(result.stdout)

在这个示例中,我们将命令和参数组合成一个字符串"ls -l",然后将shell=True传递给subprocess.run()函数,以便直接运行这个命令。这样就可以将命令和参数放在一起。

三、06.29 周六

3.1)问题01:Python运行报错处理

>>> range_obj = range(3)

>>> list1= list(ra)

raise range( range_obj

>>> list1= list(range_obj)

Traceback (most recent call last):

File "", line 1, in

TypeError: 'list' object is not callable

解决办法: 出现 TypeError: 'list' object is not callable 错误,通常是因为在代码的其他地方已经定义了一个名为 list 的变量,并且该变量的值是一个列表。这会覆盖内置的 list 函数,从而导致无法调用内置的 list 函数。

要解决这个问题,请检查你的代码,确保没有定义一个名为 list 的变量。以下是一个示例说明了这个问题以及如何修复它:

重现错误

# 误定义一个名为 list 的变量

list = [1, 2, 3]

# 尝试使用 list() 函数会引发错误

range_obj = range(3)

list1 = list(range_obj) # TypeError: 'list' object is not callable

修复方法

重命名变量:避免使用 list 作为变量名,以免覆盖内置的 list 函数。

# 避免使用 list 作为变量名

my_list = [1, 2, 3]

# 使用内置的 list() 函数将 range 对象转换为列表

range_obj = range(3)

list1 = list(range_obj)

print(list1) # 输出:[0, 1, 2]

删除错误的变量定义:如果已经定义了 list 变量,可以通过 del 关键字删除它,以恢复对内置 list 函数的访问。

# 删除误定义的 list 变量

del list

# 使用内置的 list() 函数将 range 对象转换为列表

range_obj = range(3)

list1 = list(range_obj)

print(list1) # 输出:[0, 1, 2]

正确使用示例

确保没有覆盖 list 函数,并正确使用它将 range 对象转换为列表。

# 确保没有覆盖内置的 list 函数

# 不要定义变量名为 list

range_obj = range(3)

list1 = list(range_obj)

print(list1) # 输出:[0, 1, 2]

通过确保不使用内置函数的名称作为变量名,可以避免这种错误并正确使用 Python 的内置函数。

3.2)问题02:python3 list转字符串

假设有一个数字列表

num_list = [1, 2, 3, 4, 5]

先将数字列表中的元素转换为字符串,然后用join方法连接,用逗号分隔

result = ', '.join(map(str, num_list)) print(result)

推荐文章,android

标签: 拼多多助力网站平台 赚钱项目 拼多多助力网 首码项目 手赚APP
拼多多助力营销技巧