Python | Using 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') # Equivalent to q.put('how',block=False)

print(q.get())
print(q.get())
print(q.get())
print(q.get())
print(q.get())
# print(q.get())
# q.get(block=True, timeout=10)
q.get_nowait()

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