To add an item to a sized limited queue and block with a timeout, you can use the timeout parameter like this: try: # handle exceptoin Code language: Python ( python ) In this case, the put() method will raise the queue.Full exception if the queue is full: try: If you don’t want the put() method to block if the queue is full, you can set the block argument to False: queue.put(item, block= False) Code language: Python ( python ) Also, the call to the put() method will block until the queue has space available. Once the queue is full, you won’t be able to add an item to it. To add an item to the queue, you use the put() method like this: queue.add(item) Code language: Python ( python ) For example, the following creates a queue that can store up to 10 items: queue = Queue(maxsize= 10) Code language: Python ( python ) Adding an item to the queue To create a queue with a size limit, you can use the maxsize parameter. To create a new queue, you import the Queue class from the queue module: from queue import Queue Code language: Python ( python )Īnd use the Queue constructor as follows: queue = Queue() Code language: Python ( python ) The Queue class in the queue module implements all required locking semantics. The built-in queue module allows you to exchange data safely between multiple threads. Introduction to the Python thread-safe queue Summary: in this tutorial, you’ll learn how to use a Python thread-safe queue to exchange data safely between multiple threads.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |