Created
March 6, 2018 23:19
Revisions
-
mcstafford-git created this gist
Mar 6, 2018 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,45 @@ # variable scope, lifetime # modified from stackoverflow https://goo.gl/Gy5Ai7 import multiprocessing import os def hey(pid, message): global i global queue denominator = None try: denominator = queue.qsize() except NameError: pass print( '{}:{}/{}'.format(pid, i, denominator), message, ) i += 1 def worker_main(queue): hey(os.getpid(), 'init') while True: item = queue.get(True) hey(os.getpid(), item) i = 0 hey(os.getpid(), 'BEFORE') queue = multiprocessing.Queue() pool = multiprocessing.Pool(processes=3, initializer=worker_main, initargs=(queue, )) for i in range(5): queue.put('hello') queue.put('world') hey(os.getpid(), 'DURING') while queue.qsize(): pass hey(os.getpid(), 'AFTER') #