Thread-Safe priority queue based on event implementation (Python implementation)

Source: Internet
Author: User

Event events are a great thread-synchronization and thread-communication mechanism, and many of the Python source code is based on the event to implement a lot of thread-safe, support for concurrent, thread-communication libraries

For the heap implementation of the priority queue, see "Python implementation two fork heap and heap sorting", Python event see <python Lock, Semaphore, event implementation thread synchronization, in fact, the main attention to the use of several methods of the event, As well as the logical order of the program under multi-threaded access, there are several ways to put the event in the relevant code snippet. It's not hard to understand yourself. Look at the source bar:

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._event=Threading. Event ()defpush (self, item, priority):ifLen (self._queue)! =0:self._event.clear () while  notSelf._event.is_set (): Self._event.set () Heapq.heappush (Self._queue, (-priority, Self._index, item))#In a ternary group, the default is to construct a small top heapSelf._index + = 1#Self._event.set ()    defpop (self):ifLen (self._queue)! =0:self._event.set () whileSelf._event.is_set (): Self._event.wait () x= Heapq.heappop (Self._queue) [-1]#Reverse Outputself._event.clear ()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 ( )

Suggested to see Python socket source code and queue.py and other source of these are related to multi-threaded access to the library are based on these thread synchronization mechanism to achieve, you can write down, after all, people write more professional.

Thread-Safe priority queue based on event implementation (Python implementation)

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.