Skip to content

Instantly share code, notes, and snippets.

@arastu
Last active March 4, 2019 10:53
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 arastu/7a60af7d5a4216b31017f60797f5fe94 to your computer and use it in GitHub Desktop.
Save arastu/7a60af7d5a4216b31017f60797f5fe94 to your computer and use it in GitHub Desktop.
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()
@mosajjal
Copy link

mosajjal commented Mar 4, 2019

from the official Python docs:

join() Wait for the worker processes to exit. One must call close() or terminate() before using join().

so maybe call it to free up some memory and avoid having zombies in your process list?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment