multiprocessing ; os
import multiprocessing,os
def song(num,a,que):
# Print process number
print('song process :{}'.format(os.getpid()))
for i in range(num):
a=a+1
# Add data to the queue
que.put(a)
def dance(num,a,que):
# Print process number
print('dance process :{}'.format(os.getpid()))
while True:
# Take data out of the queue
a=que.get()
print(a)
# When the queue is empty que.empty()=True, End the operation of fetching data from the queue
if que.empty():
print(' End queue ')
break
def main():
a = 0
# Create a queue , Realize interprocess communication
que=multiprocessing.Queue(10)
# Instantiate two processes , And start the
#target Pointing function ,args Followed by parameters in tuple format , Pass it into the function as an actual parameter
multiprocessing.Process(target=song,args=(10,a,que)).start()
multiprocessing.Process(target=dance, args=(10,a,que)).start()
# Print process number
print(' The main process :{}'.format(os.getpid()))
que.close()
if __name__ =='__main__':
main()
Print the results :