Thread's queue

Source: Internet
Author: User

Thread's queue

In Python, queues are the most common form of exchanging data between threads. The queue module is a module that provides queued operations, although it's easy to use, but if you're not careful, there are some surprises.

create a ' queue ' object
Import queue
Q = queue. Queue (3) #Queue (Maxsizse = 3)

The Queue.queue class is a synchronous implementation of a queue. The queue length can be unlimited or limited. The queue length can be set through the optional parameter maxsize of the queue's constructor. If MaxSize is less than 1, it means that the queue length is infinite.

put a value in the queue

Q.put (10) calls the put () method of the queue object to insert an item at the end of the team. Put () has two parameters, the first item is required, the value of the inserted item, the second block is an optional parameter, and the default is 1. If the queue is currently empty and the Block is the 1,put () method, the calling thread pauses until a data cell is vacated. If the block is the 0,put method, the full exception is thrown.

Q.put (3)
Q.put (' Zhang ')
Q.put ([' Zhang ', ' Li '])
# q.put (6,block=false)
Print (Q.get ())
Q.put (6,block=true,timeout=2)
# Q.put (8)

The block defaults to True when the queue is full. So when you insert data into a full queue, the queue blocks and the thread snaps. When the Block=false is in place, an error occurs when you put the program again. In the timeout parameter setting, q.put (6,block=true,timeout=2) means that when the queue has not been removed for two seconds, the program will get an error. In an attempt to

Thread's queue

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.