Issue
When working with Python's asyncio
package, I've noticed that I can't step into any code of its tasks.Task
class. For example, when the calling code invokes the class's constructor, my next 'step into' get's me into a get_debug()
function outside the class. After that, I return to the calling code with an initialised Task
object. I've observed similar behaviour with Task.__next_step()
: I'll just step into code that gets called by this method.
All Python versions (3.9, 3.10), IDEs (PyCharm, Visual Studio Code) and OSs (macOS, Windows) that I tested showed the same issue.
Does anyone know the reason for the debugger’s strange behaviour and, possibly, how to overcome it?
The call_soon()
in the last line of the screenshot is issued from within Task.__init__
. However, as you can see, the debugger never stepped into the initializer.
Update: Surprisingly, with Python 3.6 (Pythonista on iPadOS) I can step into Task.__init__
from base_events.BaseEventLoop.create_task()
.
Solution
The implementation of Task
is in C (where available). The debugger cannot step into C code.
You can see this in the asyncio.tasks
module:
class Task(futures._PyFuture):
...
_PyTask = Task
try:
import _asyncio
except ImportError:
pass
else:
# _CTask is needed for tests.
Task = _CTask = _asyncio.Task
That last line shows the Python implementation being overridden by the C implementation.
You can verify which implementation you have by inspecting the __module__
attribute of Task
. e.g.
import asyncio
print(asyncio.Task.__module__)
A pure Python implementation will print asyncio.tasks
. The C implementation will print _asyncio
.
Answered By - Dunes
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.