The Python queue module uses

Source: Internet
Author: User

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 (maxsize = 10)
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 put (item[, block[, timeout])

Put the item in the queue.

    1. If the optional parameter is block true and timeout is an empty object (by default, blocking calls, no timeouts).
    2. If timeout is a positive integer, it blocks the calling process up to timeout seconds, and throws a full exception (a blocking call with a timeout) if there is no empty space available.
    3. If block is false, throw the full exception immediately if there is free space available to put the data in the queue

Its non-blocking version is put_nowait equivalent toput(item, False)

Take a value out of the queue
Q.get () Get ([block[, timeout]])

The Get () method of the call queue object is removed from the team header and returns an item. The optional parameter is block, which is true by default. If the queue is empty and the Block is True,get (), the calling thread is paused until a project is available. If the queue is empty and the block is false, the queue throws an empty exception.

Removes from the queue and returns a data. Block with timeout parameter same put method

Its non-blocking method is ' get_nowait () ' equivalent toget(False)

The Python queue module has three types of queues and constructors:
1, the Python queue module FIFO queuing first-out. Class Queue.queue (MaxSize)
2, LIFO similar to the heap, that is, advanced after out. Class Queue.lifoqueue (MaxSize)
3, there is a priority queue level lower the more first out. Class Queue.priorityqueue (MaxSize)

Common methods in this package (q = Queue.queue ()):
Q.qsize () returns the size of the queue
Q.empty () returns True if the queue is empty, and vice versa false
Q.full () returns True if the queue is full, otherwise false
Q.full corresponds to maxsize size
Q.get ([block[, timeout]]) Get queue, timeout wait time
Q.get_nowait () quite q.get (False)
Non-blocking Q.put (item) write queue, timeout wait time
Q.put_nowait (item) quite Q.put (item, False)


Q.task_done () After completing a work, sends a signal to the queue that the task has completed, each get () call gets a task, and the next Task_done () call tells the queue that the task has been processed. If the current join () is blocking, it resumes execution when all the tasks in the queue have been processed (that is, every task queued by a put () call has a corresponding task_done () call).

Q.join () actually means waiting until the queue is empty before doing anything else. Blocks the calling thread until all tasks in the queue are processed.

As long as there is data queued, the number of unfinished tasks increases. The number of unfinished tasks is reduced when the consumer thread calls Task_done () (meaning that there is a consumer getting the task and completing the task). When the number of unfinished tasks drops to 0,join () is unblocked.

Advanced First Out:
Import= queue.queue (maxsize=5) for in range (5):    q.put (i)   While not Q.empty ():    print q.get ()

Results:

0 1 2 3 4
View CodeAdvanced Post-out:
Q = queue.lifoqueue () for in range (5):    q.put (i)and not q.empty ():     print q.get ()

Results:

4 3 2 1 0
View CodePriority level:
#Priority QueueImportQueueImportThreadingclassJob (object):def __init__(self, Priority, description): Self.priority=Priority Self.description=DescriptionPrint 'Job:', Descriptionreturn    def __cmp__(Self, Other):#need to add this comparison function,        returnCMP (self.priority, other.priority)#Return Negative if x<y, zero if x==y, positive if x>y.Q=Queue.priorityqueue () q.put (Job (3,'Mid-level Job')) Q.put (Job (10,'Low-level Job')) Q.put (Job (1,'High-level Job'))defprocess_job (q): whileTrue:next_job=Q.get ()Print 'For :', Next_job.description q.task_done () workers= [Threading. Thread (Target=process_job, args=(q,)), threading. Thread (Target=process_job, args=(q,))] forWinchWorkers:w.setdaemon (True)#Daemon ProcessW.start () q.join ( )
View Code

Operation Result:

job:mid-levelJobjob:low-levelJobjob:high-leveljobfor: high-levelJob  for: mid-levelJobfor: Low-level job
View Code a bit more complicated.

Implement a thread that constantly generates a random number into a queue (consider using the queue this module)
Implement a thread that constantly takes out an odd number from the top of the queue
Implement another thread to continuously remove even numbers from the queue above

#!/usr/bin/python#Coding=utf-8#__author__= ' Dahu '#data=2017-#Importrandom, threading, time fromQueueImportQueue#Producer ThreadclassProducer (Threading. Thread):def __init__(self, T_name, queue):#Threading. Thread.__init__ (self, name=t_name)Super (Producer,self).__init__(Name=t_name)#Two can be, in favor of this one.Self.data =QueuedefRun (self): forIinchRange (5):#randomly generates 10 numbers that can be modified to any sizeRandomnum = Random.randint (1, 20)            Print "%s:%s is producing%d to the queue!"%(Time.ctime (), Self.getname (), Randomnum) self.data.put (randomnum)#to queue data sequentiallyTime.sleep (1)        Print "%s:%s finished!"%(Time.ctime (), Self.getname ())#Consumer ThreadclassConsumer_even (Threading. Thread):def __init__(self, T_name, queue):#Threading. Thread.__init__ (self, name=t_name)Super (Consumer_even, self).__init__(name=t_name) Self.data=QueuedefRun (self): while1:            Try: Val_even= Self.data.get (1, 5)#get (self, block=true, timeout=none), 1 is blocking wait, 5 is timeout 5 seconds                ifVal_even% 2 = =0:Print "%s:%s is consuming.%d of the queue is consumed!"%(Time.ctime (), Self.getname (), Val_even) Time.sleep (2)                Else: Self.data.put (Val_even) time.sleep (2)            except:#wait for input, more than 5 seconds to report abnormal                Print "%s:%s finished!"%(Time.ctime (), Self.getname ()) Breakclassconsumer_odd (Threading. Thread):def __init__(self, T_name, queue): Threading. Thread.__init__(Self, name=t_name) Self.data=QueuedefRun (self): while1:            Try: Val_odd= Self.data.get (1, 5)                ifVal_odd% 2! =0:Print "%s:%s is consuming.%d of the queue is consumed!"%(Time.ctime (), Self.getname (), val_odd) Time.sleep (2)                Else: Self.data.put (val_odd) time.sleep (2)            except:                Print "%s:%s finished!"%(Time.ctime (), Self.getname ()) Break#Main ThreaddefMain (): Queue=Queue () producer= Producer ('Pro.', queue) Consumer_even= Consumer_even ('Con_even.', queue) consumer_odd= Consumer_odd ('con_odd.', queue) Producer.start () Consumer_even.start () Consumer_odd.start () Producer.join () Consumer_even.join () Consumer_odd.join ()Print 'All Threads terminate!'if __name__=='__main__': Main ()
View Code

Results:

/usr/bin/python2.7/home/dahu/pycharmprojects/spiderlearning/request_lianxi/T9.queue.thread.pyTue A  -: A: -  .: Pro. is producing theto the queue!Tue A  -: A: -  .: Con_odd. is consuming. the inchThe queue is consumed!Tue A  -: A: -  .: Pro. is producing -to the queue!Tue A  -: A: -  .: Pro. is producing2to the queue!Tue A  -: A: -  .: Con_odd. is consuming. - inchThe queue is consumed!Tue A  -: A: -  .: Pro. is producing theto the queue!Tue A  -: A: in  .: Con_even. is consuming.2 inchThe queue is consumed!Tue A  -: A: in  .: Con_odd. is consuming. the inchThe queue is consumed!Tue A  -: A: in  .: Pro. is producing -to the queue!Tue A  -: A: -  .: Pro. finished!Tue A  -: A: to  .: Con_even. is consuming. - inchThe queue is consumed!Tue A  -: A: -  .: Con_odd. finished!Tue A  -: A: -  .: Con_even. finished!All threads Terminate!Process finished with exit code0

The Python queue module uses

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.