Skip to content

Instantly share code, notes, and snippets.

@plutec
Created May 5, 2016 11:27
Show Gist options
  • Save plutec/79baf92d7b7777a80819410325839740 to your computer and use it in GitHub Desktop.
Save plutec/79baf92d7b7777a80819410325839740 to your computer and use it in GitHub Desktop.
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