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