Advertisement
rfmonk

multiprocessing_queue.py

Feb 8th, 2014
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.71 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. # this is from The Python
  4. # Standard Library by example
  5. # ISBN13: 9780321767349
  6.  
  7. import multiprocessing
  8.  
  9.  
  10. class MyFancyClass(object):
  11.  
  12.     def __init__(self, name):
  13.         self.name = name
  14.  
  15.     def do_something(self):
  16.         proc_name = multiprocessing.current_process().name
  17.         print 'Doing something fancy in %s for %s!' % \
  18.             (proc_name, self.name)
  19.  
  20.  
  21. def worker(q):
  22.     obj = q.get()
  23.     obj.do_something()
  24.  
  25. if __name__ == '__main__':
  26.     queue = multiprocessing.Queue()
  27.  
  28.     p = multiprocessing.Process(target=worker, args=(queue,))
  29.     p.start()
  30.  
  31.     queue.put(MyFancyClass('Fancy Man'))
  32.  
  33.     # Wait for the worker to finish
  34.     queue.close()
  35.     queue.join_thread()
  36.     p.join()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement