【发布时间】:2019-01-25 07:41:01
【问题描述】:
告诉我,我错在哪里?我在Tornado 服务器的上下文中创建了一个TornadoScheduler。分配一个监听器和一个执行函数。但我得到的并不完全是我需要的。
from tornado.concurrent import return_future, run_on_executor
class Users:
@return_future
@run_in_executor
def save(self, callback=None):
some code
callback(some data)
scheduler.add_listener(_scheduler_listener, apscheduler.events.EVENT_ALL)
......
async def processing(event: JobEvent):
data = await Users.get_all_users() <-- comunicate with DB
if isinstance(data.result(), Exception):
raise data.result()
.....
done = await users.save() <-- comunicate with DB
.....
def _scheduler_listener(event: JobEvent):
asyncio.ensure_future(processing(event))
调度程序启动任务,代码 512 - 添加、32768 - 提交和 4096 - 执行的所有 3 个事件都到达侦听器,但它 asyncio.ensure_future() 只执行了 2 次,分别是 512 和 32768 代码。
我使用Tornado 5、TornadoScheduler。当我创建一个作业时,调度程序将代码 512 发送到侦听器,然后是代码 32768,然后是代码 4096。但是,当代码 4096 到达时,asyncio.ensure_future 不会执行。当我按Crtl+C 时,会显示控制台:
RuntimeWarning: coroutine 'processing' was never awaited
2019-01-30 19:04:24,473 - asyncio - ERROR - Task was destroyed but it is pending! task: <Task pending coro=<processing() running at ....
是什么原因?
【问题讨论】:
-
请提供一个独立的例子。从这个 sn-p 很难找出您可能遇到的任何问题。
-
@Alex Grönholm 我稍微更新了这个问题,现在我认为这是一个问题。很可能我做错了什么
-
@Alex Grönholm 当我在 ThreadPoolExec().submit(loop.run_forever()) 中创建一个新的事件循环(asyncio.new_event_loop()) 时 b>,并在其中执行 asyncio.ensure_future(coro, loop=new created loop),一切正常。
标签: python-3.x tornado python-asyncio apscheduler