-
-
Save AndreiPashkin/be76c7ffbe29e2c337d348376fc9d627 to your computer and use it in GitHub Desktop.
Multiprocessing graceful stop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import multiprocessing as mp | |
import queue as queuelib | |
N_PROCESSES = 4 | |
def worker(queue: mp.Queue, stop: mp.Event): | |
print(f"Start job: {mp.current_process().name}") | |
while True: | |
if stop.is_set(): | |
print('Will stop by signal') | |
break | |
try: | |
value = queue.get(timeout=0.5) | |
except (queuelib.Empty, mp.TimeoutError): | |
continue | |
result = value ** 2 | |
print(f'[{mp.current_process().name}] Result = {result}') | |
print('End job') | |
if __name__ == '__main__': | |
mp.set_start_method('spawn') | |
stop = mp.Event() | |
queue = mp.Queue() | |
processes = [mp.Process(target=worker, args=(queue, stop)) | |
for x in range(N_PROCESSES)] | |
for p in processes: | |
p.start() | |
for n in range(1000): | |
queue.put(n) | |
time.sleep(1) | |
stop.set() | |
for p in processes: | |
p.join() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment