The Use of Queues | Teach You to Get Started With Python One Hundred and Eighth

The Use of Queues | Teach You to Get Started With Python One Hundred and Eighth

This section describes the difference between queues for inter-process communication and inter-thread communication.

The Use of Queues.use of queues


import multiprocessing, queue

# q1 = multiprocessing.Queue () # Interprocess communication
# q2 = queue.Queue () # Inter-thread communication


# When creating a queue, you can specify a maximum length. The default value is 0, which means unlimited
q = multiprocessing.Queue (5)

q.put ( 'hello' )
q.put ( 'good' )
q.put ( 'yes' )
q.put ( 'ok' )
q.put ( 'hi' )

# print( q.full ()) #True
# q.put ('how') # can't put in

# block = True: means blocking, if the queue is full, wait
# timeout timeout, how long to wait before the program will error, the unit is seconds
# q.put ( 'how', block=True, timeout=5)

# q.put_nowait('how') # 等价于 q.put('how',block=False)

print(q.get())
print(q.get())
print(q.get())
print(q.get())
print(q.get())

Related Articles

Explore More Special Offers

  1. Short Message Service(SMS) & Mail Service

    50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00