Skip to content

Commit b6ed4bd

Browse files
committed
Add the service representation to managed task names
This makes it much easier when debugging to know which service spawned each task. Signed-off-by: Leandro Lucarella <[email protected]>
1 parent bc98971 commit b6ed4bd

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/frequenz/core/asyncio.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,21 +208,32 @@ def create_task(
208208
Tasks can be retrieved via the [`tasks`][frequenz.core.asyncio.Service.tasks]
209209
property.
210210
211+
Managed tasks always have a `name` including information about the service
212+
itself. If you need to retrieve the final name of the task you can always do so
213+
by calling [`.get_name()`][asyncio.Task.get_name] on the returned task.
214+
211215
Tasks created this way will also be automatically cancelled when calling
212216
[`cancel()`][frequenz.core.asyncio.Service.cancel] or
213217
[`stop()`][frequenz.core.asyncio.Service.stop], or when the service is used as
214218
a async context manager.
215219
216220
Args:
217221
coro: The coroutine to be managed.
218-
name: The name of the task.
222+
name: The name of the task. Names will always have the form
223+
`f"{self}:{name}"`. If `None` or empty, the default name will be
224+
`hex(id(coro))[2:]`. If you need the final name of the task, it can
225+
always be retrieved
219226
context: The context to be used for the task.
220227
log_exception: Whether to log exceptions raised by the task.
221228
222229
Returns:
223230
The new task.
224231
"""
225-
task = self._task_creator.create_task(coro, name=name, context=context)
232+
if not name:
233+
name = hex(id(coro))[2:]
234+
task = self._task_creator.create_task(
235+
coro, name=f"{self}:{name}", context=context
236+
)
226237
self._tasks.add(task)
227238
task.add_done_callback(self._tasks.discard)
228239

0 commit comments

Comments
 (0)