How can I terminate a running process, started using concurrent.futures? As I understand, the cancel() method is there to remove a process from the queue if it is not running. But what about killing a running process? For example, if I have a long running process, and I want to stop it when I press a Cancel button in a GUI.
Asked
Active
Viewed 4,981 times
12
Charles Brunet
- 19,947
- 21
- 80
- 121
2 Answers
2
In this case it would probably be better to use a multiprocessing.Process for a long running task.
Create a multiprocessing.Event before starting the new process. Have the child process check the status of this Event regularly, and make it exit when Event.is_set() returns True.
In your GUI code, have the callback associated with the button call set() on the Event.
Roland Smith
- 39,308
- 3
- 57
- 86
2
You may want to look at my answer to a related StackOverflow question here.
In short, there does not appear to be a simple way to cancel a running process inside a concurrent.futures.ProcessPoolExecutor. But you can accomplish it in a hacky way by killing the child processes manually.
ostrokach
- 14,836
- 7
- 69
- 87