Skip to content

Instantly share code, notes, and snippets.

@mdellavo
Created July 8, 2011 02:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mdellavo/1070957 to your computer and use it in GitHub Desktop.
Save mdellavo/1070957 to your computer and use it in GitHub Desktop.
redis-blocking-queue.py
# poller.py
import redis
import time
# under an evented wsgi server, a queue can be assigned to a response app_iterable
# to return a long-polling chunked response
def queue():
store = redis.Redis('localhost')
while True:
k, value = store.blpop(['foo'])
yield value
for i in queue():
print i
#---------------------------------------------------------
# pusher.py
mport sys
import redis
import time
import json
store = redis.Redis()
while True:
i = raw_input().strip()
if i:
data = {'ts': time.time(), 'data': i}
store.rpush('foo', json.dumps(data))
#---------------------------------------------------------
# pyramid-view.py
@view_config(name='stat')
def stat(request):
def chunked():
while True:
channel, data = redis.Store.blpop(['foo'])
rv = {'channel' : channel, 'data': data, 'ts': time.time()}
yield json.dumps(rv) + "\n"
response = Response()
response.content_type = 'application/json'
response.app_iter = chunked()
return response
@MayankFawkes
Copy link

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment