#!./uwsgi --http-socket :9090 --asyncio 100 --module tests.websockets_chat_asyncio --greenlet import uwsgi import asyncio import asyncio_redis import time import greenlet class GreenFuture(asyncio.Future): def __init__(self): super().__init__() self.greenlet = greenlet.getcurrent() self.add_done_callback(lambda f: f.greenlet.switch()) def result(self): while True: if self.done(): return super().result() self.greenlet.parent.switch() @asyncio.coroutine def redis_open(f): connection = yield from asyncio_redis.Connection.create(host='localhost', port=6379) f.set_result(connection) f.greenlet.switch() @asyncio.coroutine def redis_subscribe(f): connection = yield from asyncio_redis.Connection.create(host='localhost', port=6379) subscriber = yield from connection.start_subscribe() yield from subscriber.subscribe(['foobar']) f.set_result(subscriber) f.greenlet.switch() def ws_recv_msg(g): g.has_ws_msg = True g.switch() @asyncio.coroutine def redis_wait(subscriber, f): reply = yield from subscriber.next_published() f.set_result(reply.value) f.greenlet.switch() @asyncio.coroutine def redis_publish(connection, msg): yield from connection.publish('foobar', msg.decode('utf-8')) def application(env, sr): ws_scheme = 'ws' if 'HTTPS' in env or env['wsgi.url_scheme'] == 'https': ws_scheme = 'wss' if env['PATH_INFO'] == '/': sr('200 OK', [('Content-Type', 'text/html')]) return ("""