Skip to content

Instantly share code, notes, and snippets.

@AndreiPashkin
Forked from worldmind/grace_stop.py
Last active July 8, 2019 17:47
Show Gist options
  • Save AndreiPashkin/be76c7ffbe29e2c337d348376fc9d627 to your computer and use it in GitHub Desktop.
Save AndreiPashkin/be76c7ffbe29e2c337d348376fc9d627 to your computer and use it in GitHub Desktop.
Multiprocessing graceful stop
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