Created
May 5, 2016 11:27
-
-
Save plutec/79baf92d7b7777a80819410325839740 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import heapq | |
import threading | |
class HeapThreadSafe(object): | |
def __init__(self): | |
self.sem = threading.Semaphore(1) | |
self.heap = list() | |
def push(self, obj): | |
self.sem.acquire() | |
heapq.heappush(self.heap, obj) | |
self.sem.release() | |
def pop(self): | |
to_ret = None | |
self.sem.acquire() | |
try: | |
to_ret = heapq.heappop(self.heap) | |
except: | |
pass | |
self.sem.release() | |
return to_ret |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment