python-3.x - 为什么进程终止时没有 process.join() 返回?
问题描述
我的代码如下:
from multiprocessing import Process, Queue
task_queue = Queue(TASK_QUEUE_SIZE)
result_queue = Queue()
def worker_thread(worker_id, task_queue, result_queue):
# consume task_queue and put result into result_queue
# if task is None, it prints closed message and returns 0
...
workers = [Process(target=worker_thread, args=(i, task_queue, result_queue))
for i in range(NUM_OF_THREADS)]
for w in workers:
w.start()
# put task into task_queue, read result from result_queue...
# all done. try to exit.
print('waiting 120 seconds for workers...')
time.sleep(120) # wait long enough for workers return
for no, w in enumerate(workers):
print(f'joining worker #{no}')
w.join(1)
print(f'worker #{no} is_alive: {w.is_alive()} exitcode: {w.exitcode}')
这个程序生成这样的日志:
pending jobs: 1
worker #0 is started.
waiting 120 seconds for workers...
worker #0 got task id: 123
worker #0 got result for task id: 123
worker #0 is closed.
joining worker #0
worker #0 isalive: False exitcode: 0
如果我使用 w.join(),它将永远等待。很奇怪。你能给我一些线索吗?先感谢您。
====
更新:我发现我的代码有时会永远等待。如果日志记录是我原始帖子的形式,它将安全退出。但是,如果它像下面的 [使用 join(1)],它将永远卡住。奇怪的是,worker #0 打印了关闭的消息但没有退出。
waiting 120 seconds for workers...
worker #0 got result id: 123
worker #0 is closed.
joining worker #0
worker #0 isalive: True exitcode: None
def worker_thread(worker_id: int, task_queue, result_queue):
print(f'worker #{worker_id} is started.')
while 1:
try:
t = task_queue.get()
if t is None:
break
r = do_task(t)
if r:
result_queue.put((t, r))
except Exception as e:
print(f'[FATAL] worker #{worker_id} got unknown error on task {t}.\nReason: {type(e)} {e}')
traceback.print_exc()
continue
print(f'worker #{worker_id} is closed.')
return 0
解决方案
推荐阅读
- java - Spark 无法读取阿拉伯字符并将其替换为“?” 在阅读
- r - R unpivot 列与排名组合
- javascript - 未检测到 vuex 存储 - Quasar
- java - 应用程序更新继续,即使在使用谷歌播放核心库更新应用程序之后
- c# - 如何识别 Plugin.Ble 中的特征?
- python - SQLAlchemy - 在不同的数据库中定义外键关系
- android - “定时发布”中的“开始部署到生产”按钮有什么作用
- javascript - 如何使用正则表达式设置文本小写?
- python - 如何将字典转换为数据框?
- oracle - 如何从oracle中的语句中替换多个单词/特殊字符