-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
40 lines (34 loc) · 1.21 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import asyncio
# https://phoolish-philomath.com/asynchronous-task-scheduling-in-python.html
async def run_periodically(wait_time, coro, *args):
"""
Helper for schedule_task_periodically.
Wraps a coroutine in another coroutine that will run the
given coroutine indefinitely
:param wait_time: seconds to wait between iterations of coro
:param coro: the coroutine that will be run
:param args: any args that need to be provided to coro
"""
while True:
await coro(*args)
await asyncio.sleep(wait_time)
def schedule_task_periodically(wait_time, coro, event_loop, *args):
"""
Schedule a coroutine to run periodically as an asyncio.Task
:param wait_time: interval (in seconds)
:param coro: the coroutine that will be run
:param event_loop: the event loop used
:param args: any args needed to be provided to coro
:return: an asyncio Task that has been scheduled to run
"""
return event_loop.create_task(run_periodically(wait_time, coro, *args))
async def cancel_scheduled_task(task):
"""
Gracefully cancels a task
:type task: asyncio.Task
"""
task.cancel()
try:
await task
except asyncio.CancelledError:
pass