Created
July 8, 2011 02:03
-
-
Save mdellavo/1070957 to your computer and use it in GitHub Desktop.
redis-blocking-queue.py
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
# 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 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks