Event-based thread-safe priority queue (python implementation) and eventpython
Event Events are a good thread synchronization mechanism and thread communication mechanism. In many python source code, event-based libraries provide a lot of thread security and support concurrency and thread communication.
For the heap implementation of the priority queue, see Implementing the binary heap and heap sorting in python. For the python event, see <python lock, semaphore, and event to achieve thread synchronization>, in fact, we mainly pay attention to the usage of several event methods and the logic sequence of the program under multi-threaded access. Just put the methods of event in the relevant code segment. It's not hard to understand. Let's take a look at the source code:
Import heapqimport threading # import timeclass Item: def _ init _ (self, name): self. name = name def _ repr _ (self): return 'item ({! R })'. format (self. name) class PriorityQueue: def _ init _ (self): self. _ queue = [] self. _ index = 0 self. _ event = threading. event () def push (self, item, priority): if len (self. _ queue )! = 0: self. _ event. clear () while not self. _ event. is_set (): self. _ event. set () heapq. heappush (self. _ queue, (-priority, self. _ index, item) # store a triple. The default structure is the small top heap self. _ index + = 1 # self. _ event. set () def pop (self): if len (self. _ queue )! = 0: self. _ event. set () while self. _ event. is_set (): self. _ event. wait () x = heapq. heappop (self. _ queue) [-1] # output self in reverse order. _ event. clear () return xdef test1 (p, item, index): for I in range (3): p. push (Item (item), index) def test2 (p): for I in range (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, 'login', 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 () t4.start () t6.start () t4.join () t6.join ()
We recommend that you check the python socket source code and queue. py and other source code. These libraries involving multi-thread access are implemented based on these thread synchronization mechanisms. You can write them in imitation. After all, others write more professionally.