Created
June 27, 2015 09:35
-
-
Save MarkLavrynenko/74783a5329ce957e2bf0 to your computer and use it in GitHub Desktop.
Python process safe queue usage example and logging from multiple processes
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
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