1 回答
TA贡献1815条经验 获得超10个赞
asyncio 事件循环已经包含您尝试实现的代码 - 排序超时并等待任务提交。您需要使接口适应Scheduler底层 asyncio 功能,例如如下所示:
class Scheduler:
def __init__(self):
self._running = asyncio.Lock()
async def start(self):
pass # asyncio event loop will do the actual work
def add(self, action):
loop = asyncio.get_event_loop()
# Can't use `call_at()` because event loop time uses a
# different timer than time.time().
loop.call_later(
action.timestamp - time.time(),
loop.create_task, self._execute(action)
)
async def _execute(self, action):
# Use a lock to ensure that no two actions run at
# the same time.
async with self._running:
await action.do()
添加回答
举报