Skip to content

Instantly share code, notes, and snippets.

@MarkLavrynenko
Created June 27, 2015 09:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MarkLavrynenko/74783a5329ce957e2bf0 to your computer and use it in GitHub Desktop.
Save MarkLavrynenko/74783a5329ce957e2bf0 to your computer and use it in GitHub Desktop.
Python process safe queue usage example and logging from multiple processes
from multiprocessing import Process, Queue
from time import sleep
def func(q, cnt, log):
for i in range(0, cnt):
item = q.get()
message = "Got an item %s\n" % item
log.put(message)
if __name__ == '__main__':
cnt = 10
q = Queue()
log = Queue()
p1 = Process(target=func, args=(q, cnt, log))
p1.start()
for i in range(0, cnt):
sleep(0.1)
item = "item #%s" % i;
q.put(item)
message = "Put and item %s\n" % item
log.put(message)
p1.join()
with open("log.txt", "w") as file:
while not log.empty():
record = log.get()
file.write(record)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment