multiprocessing.Queue 关闭以后再 get() 为什么会卡住?

查看 16|回复 0
作者:sduoduo233   
import multiprocessing as mp
def p1(q):
    print("p1 start")
    print(q.get())
    print("p1 end")
if __name__ == "__main__":
    q = mp.Queue()
    q.close()
    mp.Process(target=p1, args=(q,)).start()
    print("closed")
输出,然后就卡在 q.get() 了:
closed
p1 start
但是如果在主进程 q.get() 就会正常的报错:
import multiprocessing as mp
def p1(q):
    print("p1 start")
    print(q.get())
    print("p1 end")
if __name__ == "__main__":
    q = mp.Queue()
    q.close()
    q.get() #
Traceback (most recent call last):
  File "/home/username/test.py", line 11, in
    q.get()
  File "/usr/lib64/python3.12/multiprocessing/queues.py", line 100, in get
    raise ValueError(f"Queue {self!r} is closed")
ValueError: Queue  is closed
您需要登录后才可以回帖 登录 | 立即注册

返回顶部