Last active
February 5, 2017 14:56
-
-
Save ccaspers/b627f7779dd2b12afa3e1cb55897f208 to your computer and use it in GitHub Desktop.
basic multiprocessing with python 3.6. workers can be stopped externally via events.
This file contains 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
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
import multiprocessing as mp | |
from time import sleep | |
def task(exit_evt, id): | |
while not exit_evt.is_set(): | |
pass | |
print("Worker %s exiting" % id) | |
def main(): | |
events = [] | |
processes = [] | |
for i in range(2): | |
exit_event = mp.Event() | |
events.append(exit_event) | |
worker_args = [exit_event, i] | |
processes.append(mp.Process(target=task, args=worker_args)) | |
sleep(1) | |
[p.start() for p in processes] | |
for evt in events: | |
sleep(1) | |
evt.set() | |
[p.join() for p in processes] | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment