APScheduler侦听器,请不要运行asyncio.ensure_future

时间:2019-01-25 07:41:01

标签: python-3.x tornado python-asyncio apscheduler

告诉我,我哪里错了?我在TornadoScheduler服务器的上下文中创建了Tornado。分配一个侦听器和一个执行函数。但是我并没有得到我真正需要的东西。

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))

调度程序开始执行任务,并且所有3个事件(带有添加的512,提交的32768和执行的4096代码)都到达侦听器,但是asyncio.ensure_future()仅执行了2次,即512和32768代码。 我使用Tornado 5TornadoScheduler。创建作业时,调度程序将代码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 ....

原因是什么?

1 个答案:

答案 0 :(得分:1)

要运行异步函数,必须使用AsyncIOScheduler并将您的callable声明为(本机)协程函数。