MCPcopy
hub / github.com/ray-project/ray / run_background_task

Function run_background_task

python/ray/_common/utils.py:160–186  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

158
159
160def 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

Callers 7

test_run_background_taskFunction · 0.90
__init__Method · 0.90
create_check_raylet_taskFunction · 0.90
__init__Method · 0.90
_recover_running_jobsMethod · 0.90
submit_jobMethod · 0.90
monitor_eventsFunction · 0.90

Calls 3

get_or_create_event_loopFunction · 0.85
addMethod · 0.45
add_done_callbackMethod · 0.45

Tested by 1

test_run_background_taskFunction · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…