Skip to content

Instantly share code, notes, and snippets.

@psobot
Created November 9, 2012 18:05
Show Gist options
  • Save psobot/4047223 to your computer and use it in GitHub Desktop.
Save psobot/4047223 to your computer and use it in GitHub Desktop.
Buffered Read Queue for Multiprocessing in Python
import Queue
import multiprocessing
import threading
class BufferedReadQueue(Queue.Queue):
def __init__(self, lim=None):
self.raw = multiprocessing.Queue(lim)
self.__listener = threading.Thread(target=self.listen)
self.__listener.setDaemon(True)
self.__listener.start()
Queue.Queue.__init__(self, lim)
def listen(self):
try:
while True:
self.put(self.raw.get())
except:
pass
@property
def buffered(self):
return self.qsize()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment