12.2 multi-thread communication: queue, 12.2 queue

Source: Internet
Author: User

12.2 multi-thread communication: queue, 12.2 queue
Queue:

  • What is a queue? It is a special structure, similar to a list. However, just like queuing, once the elements in the queue are taken out, they will be deleted from the queue.
  • The queue can be used for inter-thread communication.
  • How to Use queue. Queue for a thread to communicate with other types of objects:
    • 1. Create a Queue object: Object = queue. Queue (x), x is the Queue capacity, x can be left blank, no capacity limit by default,
    • 2. get () allows the thread to get an element from the queue. If the queue is empty, get will wait. get can set the timeout parameter, which is the waiting time.
    • 3. put () can put an element into the Queue [Default Queue is first-in-first-out, first-in-first-out will be removed]. If the Queue is full, put will wait, you can set the timeout parameter for put, which is the waiting time.

[Example below: the sender thread sends the diameter to the recvder thread, and the recvder calculates the perimeter]

Import threading, time, queue, randomdef sender (): # sender sends diameter while True: x = random. randint (1, 10) print ("send done:", x) q. put (x) # put a random number time every second. sleep (1) # Put an adef recvder () every second: # recvder calculates the perimeter while True: x = q. get () print ("recv done:", x * 3.14) # retrieve an element every second and calculate the result time. sleep (1) q = queue. queue () t1 = threading. thread (target = sender) t2 = threading. thread (target = recvder) t1.start () t2.start () t1.join () t2.join ()

 

  • The Queue object already contains the necessary locks, so don't worry about errors.
Import threading, time, queue, randomdef sender (): # sender sends diameter while True: x = random. randint (1, 10) print ("send done:", x) q. put (x) # put a random number time every second. sleep (1) # Put an adef recvder () every second: # recvder calculates the perimeter while True: x = q. get () print (threading. current_thread (), "recv done:", x * 3.14) # retrieve an element every second and calculate the result time. sleep (2) q = queue. queue () t1 = threading. thread (target = sender) t2 = threading. thread (target = recvder) t3 = threading. thread (target = recvder) t1.start () t2.start () t3.start () t1.join () t2.join () t3.join ()

 

Note:

Queue capacity limit:

Timeout settings:

 

 

Other related functions of the Queue [set q As a Queue object ]:
  • Q. qsize (): returns the number of elements in the current queue.
  • Q. empty (): checks whether the queue is empty and returns a Boolean value.
  • Q. full (): determines whether the queue is full. A boolean value is returned.

 

  • Q. get_nowait (): Use get () directly. If no element exists in the queue, the wait will be blocked. If no element exists in the queue after get_nowait () is used, an error will be reported.
  • Q. put_nowait (): use put () directly. If the queue is full, the wait will be blocked. If put_nowait () is used, an error will be returned if the queue is full.

 

  • Q. task_done (): After a task is completed, the task_done () function sends a signal to the queue that the task has completed. The function is similar to that of a worker bridge that can only bear one person, A came and found B on the bridge, so A could not get on the bridge, so he was waiting. After B crossed the bridge, he shouted at him. Then he knew B had crossed the bridge.] [q. task_done is mainly used with q. join () used in combination]
  • Q. join (): Actually, it means to wait until the queue is empty and then perform other operations [Call task_done after each get operation until all queues are empty. Then, the following join operation is executed]
Import threading, queue, time "" This example is: the manufacturer and the driver agreed that the production is full of three, the driver will pull, and pull one by one, only when three are pulled, manufacturers continue to produce "" def producer (): # manufacturers while True: for I in range (3): q. put (I) start_time = time. time () q. join () # The result shows that join is blocked by the factory thread print ("wait time:", time. time ()-start_time) # Run def driver (): # The old driver while True: for I in range (3 ): print (q. get () q. task_done () print ("") time. sleep (2) q = queue. queue () t1 = threading. thread (target = producer) t2 = threading. thread (target = driver) t1.start () t2.start () t1.join () t2.join ()

 

In addition to the queue, there are other queues. Below are some of the common Queues:
  • Queue is the first-in-first-out Queue:

  • LifoQueue is the queue of the first-in-first-out mode:

  • PriorityQueue is determined by the priority specified when an element is loaded to determine the order of the elements:
    • Creation Method: queue object = queue. PriorityQueue ()
    • Priority is a small priority, but it cannot be mixed. str can only be sorted together with str, and int can only be sorted together with int.
    • The put parameter of PriorityQueue is a tuples in the format of queue object. put (priority, data ))

  • Deque is a dual-end queue that allows both the first-in, first-out, and then-out, that is, both ends can be
    • Because the double-end queue is not practical, in fact there is no difference with the list, here do not elaborate, you can refer to: https://www.cnblogs.com/zhenwei66/p/6598996.html

 

Related Article

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: info-contact@alibabacloud.com 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.