python - python多处理:睡眠语句被杀?
问题描述
我有以下代码:
import multiprocessing, datetime
def Unit_Task_Function(Argument):
print(f"Unit of work {Argument} starting {datetime.datetime.now().strftime('%Y-%m-%d : %H-%M-%S')}")
sleep(2*random.random())
print(f"Unit of work {Argument} ending {datetime.datetime.now().strftime('%Y-%m-%d : %H-%M-%S')}")
if __name__ == "__main__": # Allows for the safe importing of the main module
__spec__ = "ModuleSpec(name='builtins', loader=<class '_frozen_importlib.BuiltinImporter'>)"
print(f"There are {multiprocessing.cpu_count()} CPUs on this machine")
max_para_processes_at_any_time = 5
pool = multiprocessing.Pool(max_para_processes_at_any_time)
iterable_arguments = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'M', 'N']
results = pool.map_async(Unit_Task_Function, iterable_arguments)
pool.close()
pool.join()
输出是:
In [18]: run code.py
There are 8 CPUs on this machine
Unit of work A starting 2020-01-27 : 12-26-02
Unit of work B starting 2020-01-27 : 12-26-02
Unit of work C starting 2020-01-27 : 12-26-02
Unit of work D starting 2020-01-27 : 12-26-02
Unit of work E starting 2020-01-27 : 12-26-02
Unit of work F starting 2020-01-27 : 12-26-02
Unit of work G starting 2020-01-27 : 12-26-02
Unit of work H starting 2020-01-27 : 12-26-02
Unit of work M starting 2020-01-27 : 12-26-02
Unit of work N starting 2020-01-27 : 12-26-02
为什么第二个打印语句从未完成?这与所谓的“守护进程”进程有关吗?如何重写代码以使其工作?
解决方案
你错过了一些进口。这会导致工作人员中的导入错误,您看不到。导入这些:
import random
from time import sleep
你这个输出:
There are 8 CPUs on this machine
Unit of work A starting 2020-01-27 : 18-48-21
Unit of work B starting 2020-01-27 : 18-48-21
Unit of work C starting 2020-01-27 : 18-48-21
Unit of work D starting 2020-01-27 : 18-48-21
Unit of work E starting 2020-01-27 : 18-48-21
Unit of work E ending 2020-01-27 : 18-48-21
Unit of work F starting 2020-01-27 : 18-48-21
Unit of work A ending 2020-01-27 : 18-48-22
Unit of work G starting 2020-01-27 : 18-48-22
Unit of work C ending 2020-01-27 : 18-48-22
Unit of work H starting 2020-01-27 : 18-48-22
Unit of work D ending 2020-01-27 : 18-48-22
Unit of work M starting 2020-01-27 : 18-48-22
Unit of work G ending 2020-01-27 : 18-48-22
Unit of work N starting 2020-01-27 : 18-48-22
Unit of work B ending 2020-01-27 : 18-48-22
Unit of work M ending 2020-01-27 : 18-48-23
Unit of work F ending 2020-01-27 : 18-48-23
Unit of work N ending 2020-01-27 : 18-48-24
Unit of work H ending 2020-01-27 : 18-48-24
提示:pool.map()
在切换到 之前使用 first 查看错误消息 pool.map_async()
。
或者,提供一个重新引发异常的错误回调函数:
def on_error(err):
raise err
results = pool.map_async(Unit_Task_Function,
iterable_arguments,
error_callback=on_error)
推荐阅读
- python - 随机森林和集成学习器给出了相似的准确度
- vscode-settings - 我对 VScode 中的终端有疑问
- c++ - 对于具有重复字符的字符串,字符串的排列失败
- apache-flink - Q.Apache Flink:在initializeState期间如何获取当前key
- reactjs - ReactJs输入第一个值变空
- reactjs - 在 React 函数组件的外部使用变量是一种反模式吗?
- javascript - 试图为 Instagram 制作一个 Auto Like Bot
- spring-boot - 钥匙斗篷弹簧靴总是给找不到指定孩子的公钥
- php - 运行 npm install 时如何解决“npm ERR!code 1”
- powershell - 从 powershell 连接 GKE 集群的别名命令