8

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?

RainJay
  • 81
  • 1
  • 3
  • 1
    Every task has method `Task.get_stack()`. Maybe this is what you looking for. – Qeek Aug 02 '17 at 08:05
  • 2
    @Qeek Thanks for idea. ```[*map(asyncio.Task.print_stack, asyncio.Task.all_tasks())]``` works fine. – RainJay Aug 02 '17 at 08:19
  • 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.9 – gelonida Sep 20 '19 at 10:01

1 Answers1

2

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.)

J_H
  • 17,926
  • 4
  • 24
  • 44
  • 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)? – GaryO Jul 11 '18 at 21:27
  • 1
    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. – J_H Jul 12 '18 at 05:27