Last active
May 7, 2019 20:23
-
-
Save atarola/277735d8abecbecc594f630eeb1fbaad to your computer and use it in GitHub Desktop.
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 python | |
import argparse | |
import os | |
import random | |
import time | |
from multiprocessing import Process, JoinableQueue, Lock | |
# | |
# do a thing in multiple processes | |
# | |
def main(): | |
options = _get_options() | |
queue = JoinableQueue() | |
lock = Lock() | |
consumers = [] | |
for i in range(10): | |
proc = Process(target=consumer, args=(queue, lock)) | |
proc.daemon = True | |
consumers.append(proc) | |
for worker in workers: | |
consumers.start() | |
for i in range(100): | |
queue.put(i) | |
queue.join() | |
def consumer(queue, lock): | |
with lock: | |
print('Starting consumer => {}'.format(os.getpid())) | |
while True: | |
name = queue.get() | |
time.sleep(random.randint(0, 10)) | |
with lock: | |
print('{} got {}'.format(os.getpid(), name)) | |
queue.task_done() | |
def _get_options(): | |
"""parse the command line options""" | |
parser = argparse.ArgumentParser(description="") | |
return parser.parse_args() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment