Skip to content

Instantly share code, notes, and snippets.

@pythonlessons
Created September 15, 2020 13:57
Show Gist options
  • Select an option

  • Save pythonlessons/b61630ff2a3c653676dbc6e03faf84fa to your computer and use it in GitHub Desktop.

Select an option

Save pythonlessons/b61630ff2a3c653676dbc6e03faf84fa to your computer and use it in GitHub Desktop.
multiprocessing_queue_simple.py
from multiprocessing import Process, Queue
import time
def send_to_queue(q, mylist):
# function to put elements into Queue
for num in mylist:
q.put(num)
def pick_from_queue(q, mylist):
# function to print queue elements
print("Queue elements:")
t1 = time.time()
for num in mylist:
result = q.get()
#print(result)
print("Queue is now empty!", time.time()-t1)
if __name__ == "__main__":
# input list
mylist = [i for i in range(1000000)]
# creating multiprocessing Queue
q = Queue()
# creating new processes
p1 = Process(target=send_to_queue, args=(q, mylist))
p2 = Process(target=pick_from_queue, args=(q, mylist))
# running process p1 and p2
p1.start()
p2.start()
# if we need waiting p1 and p2 process to finish their job
#p1.join()
#p2.join()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment