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()
添加回答
舉報