python - ProcessPoolExecutor 工作人员具有相同的“线程 ID”?
问题描述
在使用多处理器池时查看线程 ID,发现它们都具有相同的线程 ID,但进程 ID 不同。使用线程池时,工作人员具有不同的线程 ID,但进程 ID 相同(如预期的那样),但我没想到在进程池中所有工作人员都具有相同的线程 ID。
def identify_self():
import threading
import os
import time
time.sleep(random())
print(f"Thread={threading.get_ident()}, Process={os.getpid()}")
time.sleep(3)
print("MP:")
with ProcessPoolExecutor(max_workers=10) as executor:
for i in range(10):
executor.submit(identify_self)
print("MT:")
with ThreadPoolExecutor(max_workers=10) as executor:
for i in range(10):
executor.submit(identify_self)
输出:
MP:
Thread=4584541632, Process=56989
Thread=4584541632, Process=56996
Thread=4584541632, Process=56998
Thread=4584541632, Process=56993
Thread=4584541632, Process=56990
Thread=4584541632, Process=56991
Thread=4584541632, Process=56992
Thread=4584541632, Process=56995
Thread=4584541632, Process=56997
Thread=4584541632, Process=56994
MT:
Thread=123145380663296, Process=56985
Thread=123145427959808, Process=56985
Thread=123145396428800, Process=56985
Thread=123145391173632, Process=56985
Thread=123145401683968, Process=56985
Thread=123145406939136, Process=56985
Thread=123145422704640, Process=56985
Thread=123145385918464, Process=56985
Thread=123145417449472, Process=56985
Thread=123145412194304, Process=56985
当 processpool 工作人员具有相同的 threadid 时,这意味着什么?它对 GIL 有影响吗?
解决方案
推荐阅读
- python-3.x - 函数中的 dice_values 没有返回,有人可以解释我的错误吗
- r - ggplot2:多面图中的多条趋势线
- c++ - C++回调定时器实现
- c# - EF 快速调用并从存储过程返回,但随后将其转换为列表需要 20 秒来处理 1000 条记录
- ruby-on-rails - 如何解决 `false.blank?` 在 ruby on rails 中遇到的问题?
- php - 将 php 变量传递到 Twig - 文档抛出错误
- python - 这个 Ruby 代码在 Python 中的等价物是什么?
- python - 过滤包含元组的列表列表
- javascript - 如何在查找 Javascript 文件时解决命令提示符中的 SQL ECONNREFUSED 错误?
- c - 多子部分加法的流程API