問題描述
我想停止單個工作人員的所有線程.
I want to stop all threads from a single worker.
我有一個有 10 個工人的線程池:
I have a thread pool with 10 workers:
我的程序不會停止,其他進程會繼續工作,直到完成所有 100 次迭代.我想完全從調用 sys.exit()
的線程內部停止池.目前的寫法只會停止調用sys.exit()
的worker.
My program does not stop and the other processes continue working until all 100 iterations are complete. I want to stop the pool entirely from inside the thread that calls sys.exit()
. The way it is currently written will only stop the worker that calls sys.exit()
.
推薦答案
這不是你想要的方式,因為在工作進程中調用 sys.exit()
只會終止工人.它對父進程或其他工作進程沒有影響,因為它們是獨立的進程,提高 SystemExit
只會影響當前進程.您需要向父進程發送一個信號,告訴它應該關閉.為您的用例執行此操作的一種方法是使用 Event
在 multiprocessing.Manager
服務器:
This isn't working the way you're intending because calling sys.exit()
in a worker process will only terminate the worker. It has no effect on the parent process or the other workers, because they're separate processes and raising SystemExit
only affects the current process. You need to send a signal back the parent process to tell it that it should shut down. One way to do this for your use-case would be to use an Event
created in a multiprocessing.Manager
server:
輸出:
正如 Luke 的回答中所指出的,這里有一場競賽:不能保證所有工作人員都會按順序運行,因此 myfunction(20, ..)
可能會在之前運行例如,myfuntion(19, ..)
.20
之后的其他工作人員也可能在主進程可以對正在設置的事件采取行動之前運行.我通過在打印 i
之前添加 if not event.is_set():
調用來減小比賽窗口的大小,但它仍然存在.
As pointed out in Luke's answer, there is a race here: There's no guarantee that all the workers will run in order, so it's possible that myfunction(20, ..)
will run prior to myfuntion(19, ..)
, for example. It's also possible that other workers after 20
will run before the main process can act on the event being set. I reduced the size of the race window by adding the if not event.is_set():
call prior to printing i
, but it still exists.
這篇關于如何殺死多進程中的所有池工作人員?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!