Skip to content

Commit b07591f

Browse files
committed
use gevent AsyncResults instead of Channel to notify subscribed clients, wrap flask app with gevent wsgiserver when running as standalone, fix few imports when running standalone
1 parent b3e4975 commit b07591f

File tree

3 files changed

+11
-18
lines changed

3 files changed

+11
-18
lines changed

setup.py

-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
'greenlet==0.4.0',
3535
'gunicorn==0.17.2',
3636
'redis==2.7.2',
37-
'ujson==1.30',
3837
'gevent >= 1.0rc2'
3938
],
4039
entry_points={

tyron/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '0.2.2'
1+
__version__ = '0.2.3'

tyron/tyron.py

+10-16
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
from collections import defaultdict
2-
3-
try:
4-
import ujson as json
5-
except ImportError:
6-
import json
7-
2+
import json
83
from flask import Flask
94
import gevent
5+
from gevent.event import AsyncResult
6+
from gevent.timeout import Timeout
107
from gevent import monkey
11-
from gevent.queue import Channel
12-
from gevent.queue import Empty
138
import os
149
import logging
1510
import redis
@@ -33,7 +28,7 @@
3328
plugin = load_plugin(plugin_path)
3429
application.register_blueprint(plugin)
3530

36-
subscriptions = defaultdict(Channel)
31+
subscriptions = defaultdict(AsyncResult)
3732

3833
class RedisSub(gevent.Greenlet):
3934
"""
@@ -69,9 +64,9 @@ def handle_message(self, message):
6964
except:
7065
logger.exception('unable to parse the message %r' % message)
7166
else:
72-
gevent_channel = subscriptions[channel]
73-
while gevent_channel.getters:
74-
gevent_channel.put_nowait(data)
67+
async_result = subscriptions[channel]
68+
async_result.set(data)
69+
del subscriptions[channel]
7570

7671
def subscribe(self):
7772
connection = self.get_redis_connection()
@@ -95,10 +90,8 @@ def subscribe(channel):
9590
timeout = application.config['LONGPOLLING_TIMEOUT']
9691
try:
9792
message = subscriptions[channel].get(timeout=timeout)
98-
except Empty:
93+
except Timeout:
9994
message = application.config['TIMEOUT_RESPONSE_MESSAGE']
100-
finally:
101-
del subscriptions[channel]
10295
return message
10396

10497
@application.before_first_request
@@ -112,7 +105,8 @@ def start_subscribe_loop():
112105
gevent.spawn(pubsub.start)
113106

114107
def main():
115-
application.run()
108+
from gevent.wsgi import WSGIServer
109+
WSGIServer(('', 5000), application).serve_forever()
116110

117111
if __name__ == '__main__':
118112
main()

0 commit comments

Comments
 (0)