Schedule a task reliably to the event loop. This API is used when you don't want to cache the reference of `asyncio.Task`. For example, ``` get_event_loop().create_task(coroutine(*args)) ``` The above code doesn't guarantee to schedule the coroutine to the event loops
(coroutine: Coroutine)
| 158 | |
| 159 | |
| 160 | def run_background_task(coroutine: Coroutine) -> asyncio.Task: |
| 161 | """Schedule a task reliably to the event loop. |
| 162 | |
| 163 | This API is used when you don't want to cache the reference of `asyncio.Task`. |
| 164 | For example, |
| 165 | |
| 166 | ``` |
| 167 | get_event_loop().create_task(coroutine(*args)) |
| 168 | ``` |
| 169 | |
| 170 | The above code doesn't guarantee to schedule the coroutine to the event loops |
| 171 | |
| 172 | When using create_task in a "fire and forget" way, we should keep the references |
| 173 | alive for the reliable execution. This API is used to fire and forget |
| 174 | asynchronous execution. |
| 175 | |
| 176 | https://docs.python.org/3/library/asyncio-task.html#creating-tasks |
| 177 | """ |
| 178 | task = get_or_create_event_loop().create_task(coroutine) |
| 179 | # Add task to the set. This creates a strong reference. |
| 180 | _BACKGROUND_TASKS.add(task) |
| 181 | |
| 182 | # To prevent keeping references to finished tasks forever, |
| 183 | # make each task remove its own reference from the set after |
| 184 | # completion: |
| 185 | task.add_done_callback(_BACKGROUND_TASKS.discard) |
| 186 | return task |
| 187 | |
| 188 | |
| 189 | # Used in gpu detection |
searching dependent graphs…