Files
Python-for-Geeks/Chapter07/multiprocessing/process5.py
T
muassif 3a0dab2f58 wrapping python code in main()
The code will not give error on Windows OS if wrapped inside the main function.
2021-08-14 14:33:10 +04:00

30 lines
624 B
Python

# process5.py to use queue to exchange data
from multiprocessing import Process
from multiprocessing import Queue
def copy_data (list, myqueue):
for num in list:
myqueue.put(num)
def output(myqueue):
while not myqueue.empty():
print(myqueue.get())
def main():
mylist = [2, 5, 7]
myqueue = Queue()
p1 = Process(target=copy_data, args=(mylist, myqueue))
p2 = Process(target=output, args=(myqueue,))
p1.start()
p1.join()
p2.start()
p2.join()
print("Queue is empty: ",myqueue.empty())
print("Exiting the main process")
if __name__ == '__main__':
main()