【发布时间】:2017-07-20 20:46:47
【问题描述】:
我有以下基于 Python 的 Tornado 编写的代码:
def process_data(data):
# To something
def handler(message):
if message['type'] == 'message':
data = message['data']
IOLoop.current().spawn_callback(process_data, data)
async def main():
redis_client = RedisClient(redis_conf)
pubsub = redis_client.subscribe("CHANNEL", handler)
async def fetch_messages():
while True:
pubsub.get_message()
await gen.sleep(0.0001)
await fetch_messages()
if __name__ == "__main__":
import logging
logging.basicConfig()
parse_command_line()
tornado.web.Application(debug=options.debug)
io_loop = ioloop.IOLoop.current()
io_loop.run_sync(main)
鉴于上面的代码,我可以看到 process_data 正在被调用。但是,如果我删除 await gen.sleep(0.0001),则永远不会调用 process_data。有谁知道为什么?
【问题讨论】:
标签: python asynchronous tornado python-asyncio tornado-redis