Last active
March 4, 2019 10:53
-
-
Save arastu/7a60af7d5a4216b31017f60797f5fe94 to your computer and use it in GitHub Desktop.
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 characters
from multiprocessing import Queue, Process | |
from time import sleep | |
def reader_proc(q): | |
while True: | |
name = q.get() | |
print(name) | |
sleep(2) | |
if name == 'DONE': | |
break | |
def writer_proc(name, q): | |
q.put(name) | |
def main(): | |
names = ["Touhid", "Shadi", "Alie", "Amir"] | |
procs = [] | |
q = Queue() | |
for name in names: | |
proc = Process(target=writer_proc, args=(name, q,)) | |
procs.append(proc) | |
proc.start() | |
proc = Process(target=writer_proc, args=("DONE", q,)) | |
procs.append(proc) | |
proc.start() | |
proc = Process(target=reader_proc, args=(q,)) | |
procs.append(proc) | |
proc.start() | |
print("Hi from main process") | |
# complete the processes | |
for proc in procs: | |
proc.join() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
from the official Python docs:
so maybe call it to free up some memory and avoid having zombies in your process list?