Debug and list all coroutine pending by future in python asyncio
Asked Answered
S

1

9

I have a production code that heavily used asyncio.semaphore module which is suspected to have deadlock problem. I already found some solution of how to attach to running python code with unix signal, debug with ipdb.set_trace() and list all tasks on event loop with asyncio.Task.all_tasks(). Can I further inspect into the stack frame of each task or viewing each line of coroutine which is currently pending by futures on ipdb?

Swag answered 2/8, 2017 at 4:22 Comment(3)
Every task has method Task.get_stack(). Maybe this is what you looking for.Deedeeann
@Deedeeann Thanks for idea. [*map(asyncio.Task.print_stack, asyncio.Task.all_tasks())] works fine.Swag
just for info. In python 3.7 you can use asyncio.all_tasks() instead of asyncio.Task.all_tasks() asyncio.Task.all_tasks() is deprecated and will be removed in python 3.9Bahaism
S
3

As OP observes, further inspections may be obtained with

[*map(asyncio.Task.print_stack, asyncio.Task.all_tasks())]

(OP is certainly free to self-answer.)

Snowblind answered 4/1, 2018 at 7:54 Comment(2)
I have a lot of tasks, all created by the same code, but with different data. I'd like to see which one is the bad one that's still running when my app exits; the stack isn't very helpful to me. Is there any way to give a task a name that will be printed out by str(task) or repr(task)?Digitigrade
Well, if you define a class that inherits from Task, then you could certainly add an attribute and override repr so it displays that attribute.Snowblind

© 2022 - 2024 — McMap. All rights reserved.