Thread-Safe Priority queue (Python implementation) based on condition implementation

Source: Internet
Author: User

Condiftion can be understood as a high-level lock, which provides more advanced functionality than locks, Rlock allows us to control complex thread synchronization issues. Threadiong. Condition maintains a trivial object internally (by default, Rlock), which can be passed in as a parameter when creating a Condigtion object. Condition also provides acquire, release method, its meaning and the acquire, release method consistent, in fact, it is simply a simple call inside the corresponding method of the object. Based on this synchronization primitive, I implemented a basic simple thread-safe priority queue:

ImportHEAPQImportThreading#Import TimeclassItem:def __init__(self, name): Self.name=namedef __repr__(self):return 'Item ({!r})'. Format (self.name)classPriorityqueue:def __init__(self): Self._queue=[] Self._index=0 Self.mutex=Threading. Lock () Self.cond=Threading. Condition ()defpush (self, item, priority): Self.cond.acquire () Heapq.heappush (Self._queue, (-priority, Self._index, item))#In a ternary group, the default is to construct a small top heapSelf._index + = 1self.cond.notify ()#wake up a suspended threadself.cond.release ()defpop (self): Self.cond.acquire ()ifLen (self._queue) = = 0:#when the number of data in the queue is 0, blocking threads, to implement a thread-safe container, it is not difficult to understand the mechanism of the relevant synchronization primitives, design the logical order of the program execution (where to block, where to wake up)Self.cond.wait ()#The wait method frees the internal lock, and the thread is suspended, knowing that a notification or timeout has been received, the program continues execution when the thread is awakened and the lock is re-occupied        Else: x= Heapq.heappop (Self._queue) [-1]#Reverse Outputself.cond.release ()returnxdeftest1 (P, item, index): forIinchRange (3): P.push (item), index)deftest2 (p): forIinchRange (3):        Print(P.pop ())if __name__=='__main__': P=priorityqueue () T1= Threading. Thread (Target=test1, args= (p,'Foo', 1)) T3= Threading. Thread (Target=test1, args= (p,'Bar', 2)) T4= Threading. Thread (Target=test1, args= (p,'Ryan', 28)) T2= Threading. Thread (Target=test2, args=(P,)) T5= Threading. Thread (Target=test2, args=(P,)) T6= Threading. Thread (Target=test2, args=(P,)) T1.start () T2.start () T1.join () T2.join () T3.start () T5.start () T3.join () T5.join () t 4.start () T6.start () T4.join () T6.join ( )

I also implemented a priority queue based on event thread safety, see < thread-Safe priority queue based on condition (Python implementation) >

Thread-Safe Priority queue (Python implementation) based on condition implementation

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.