Created
December 5, 2017 11:27
-
-
Save ownport/8730bcbbbd2a6234ccc397ca53241c43 to your computer and use it in GitHub Desktop.
python | multiprocessing queue
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
# https://pymotw.com/3/multiprocessing/communication.html | |
import multiprocessing | |
class MyFancyClass: | |
def __init__(self, name): | |
self.name = name | |
def do_something(self): | |
proc_name = multiprocessing.current_process().name | |
print('Doing something fancy in {} for {}!'.format( | |
proc_name, self.name)) | |
def worker(q): | |
obj = q.get() | |
obj.do_something() | |
if __name__ == '__main__': | |
queue = multiprocessing.Queue() | |
p = multiprocessing.Process(target=worker, args=(queue,)) | |
p.start() | |
queue.put(MyFancyClass('Fancy Dan')) | |
# Wait for the worker to finish | |
queue.close() | |
queue.join_thread() | |
p.join() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment