技术周总结 2024.06.24~06.30(Python并发执行&shell并发执行)

文章目录

一、 06.26 周三

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

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

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

sql 复制代码
SELECT id, name FROM users;

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

sql 复制代码
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()函数来创建空集合或从其他可迭代对象(如列表、元组等)创建集合。

以下是一些示例用法:

  1. 创建空集合:
python 复制代码
my_set = set()
  1. 从列表创建集合:
python 复制代码
my_list = [1, 2, 3, 1, 2]
my_set = set(my_list)
print(my_set)  # 输出: {1, 2, 3}
  1. 添加元素到集合:
python 复制代码
my_set.add(4)
print(my_set)  # 输出: {1, 2, 3, 4}
  1. 从字符串创建集合:
python 复制代码
my_string = "hello"
my_set = set(my_string)
print(my_set)  # 输出: {'o', 'h', 'e', 'l'}

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

二、06.27 周四

2.1)问题01:shell 并发执行

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

bash 复制代码
#!/bin/bash

# Command 1 runs in the background
command1 &

# Command 2 runs in the background
command2 &

# Wait for all background processes to finish
wait

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

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

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

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

python 复制代码
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 实现多进程并发执行:

python 复制代码
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()

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

2.2.1)遇到的问题及解决:

ModuleNotFoundError: No module named 'concurrent.futures'; 'concurrent' is not a package

这错误通常是由于在Python 2中引入的concurrent.futures模块在Python 3中被删除导致的。

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

  1. 使用Python 3: 确保你的Python环境是Python 3。

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

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

python 复制代码
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()函数。下面是一个示例:

python 复制代码
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运行报错处理

python3 复制代码
>>> range_obj = range(3)
>>> list1= list(ra)
raise      range(     range_obj
>>> list1= list(range_obj)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'list' object is not callable

解决办法:

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

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

重现错误

python 复制代码
# 误定义一个名为 list 的变量
list = [1, 2, 3]

# 尝试使用 list() 函数会引发错误
range_obj = range(3)
list1 = list(range_obj)  # TypeError: 'list' object is not callable

修复方法

  1. 重命名变量 :避免使用 list 作为变量名,以免覆盖内置的 list 函数。
python 复制代码
# 避免使用 list 作为变量名
my_list = [1, 2, 3]

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

print(list1)  # 输出:[0, 1, 2]
  1. 删除错误的变量定义 :如果已经定义了 list 变量,可以通过 del 关键字删除它,以恢复对内置 list 函数的访问。
python 复制代码
# 删除误定义的 list 变量
del list

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

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

正确使用示例

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

python 复制代码
# 确保没有覆盖内置的 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)

相关推荐
m0_5831059013 分钟前
基于flask的闪现、g对象、蓝图
后端·python·flask
eclipsercp14 分钟前
《每天5分钟用Flask搭建一个管理系统》 第6章:数据库集成
数据库·python·flask
小oo呆16 分钟前
【PythonWeb开发】Flask自定义模板路径和静态资源路径
python·flask
Purepisces19 分钟前
深度学习笔记: 最详尽解释预测系统的分类指标(精确率、召回率和 F1 值)
人工智能·笔记·python·深度学习·机器学习·分类
铁匠匠匠27 分钟前
django学习入门系列之第三点《案例 小米商城二级菜单》
前端·css·经验分享·python·学习·django·前端框架
Ephemeroptera44 分钟前
IT专业入门,高考假期预习指南
java·c语言·网络·python·高考
jqrbcts1 小时前
关于发那科机器人系统升级方法
开发语言·人工智能·python·机器人·c#
2401_828014951 小时前
无线领夹麦克风哪个牌子好?揭秘领夹麦克风哪个品牌音质最好
java·c++·python·php
汀沿河1 小时前
6 矩阵相关案例
开发语言·python
辛苦cv的小hui1 小时前
Python实现接糖果小游戏
开发语言·python·pygame