This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author hauntsaninja
Recipients asvetlov, hauntsaninja, yselivanov
Date 2020-09-03.00:33:50
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <[email protected]>
In-reply-to
Content
To quote the docs (https://docs.python.org/3/library/asyncio-dev.html):

```
By default asyncio runs in production mode. In order to ease the development asyncio has a debug mode.

There are several ways to enable asyncio debug mode:

    Setting the PYTHONASYNCIODEBUG environment variable to 1.

    Using the -X dev Python command line option.

    Passing debug=True to asyncio.run().

    Calling loop.set_debug().
```

Unfortunately, this documentation is misleading (or wrong) when using asyncio.run. For instance, from a naive read of the above, I'd expect debug mode to be True for many of the following:

```
~/tmp λ cat test57.py
import asyncio
import time

async def foo():
    loop = asyncio.get_event_loop()
    time.sleep(2)
    print(loop.get_debug())

print(asyncio.coroutines._DEBUG)
asyncio.run(foo())
~/tmp λ python3 --version       
Python 3.7.3

# expected
~/tmp λ python3 test57.py
False
False

# surprising, would expect this to work
~/tmp λ python3 -X dev test57.py
True
False

# surprising, would expect this to work
~/tmp λ PYTHONASYNCIODEBUG=1 python3 test57.py
True
False

# expected
~/tmp λ vim test57.py
~/tmp λ cat test57.py
import asyncio
import time

async def foo():
    loop = asyncio.get_event_loop()
    time.sleep(2)
    print(loop.get_debug())

print(asyncio.coroutines._DEBUG)
asyncio.run(foo(), debug=True)
~/tmp λ python3 test57.py       
False
True
Executing <Task finished coro=<foo() done, defined at test57.py:4> result=None created at /Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/asyncio/base_events.py:563> took 2.003 seconds
~/tmp λ 
```

This appears to be because asyncio.run sets debug mode here: https://github.com/python/cpython/blob/4a97b1517a6b5ff22e2984b677a680b07ff0ce11/Lib/asyncio/runners.py#L42

We should either change asyncio.run to use the existing value of debug mode if left unspecified, or we should update the documentation to explicitly call out that the environment variable and command line flag will not work when using asyncio.run
History
Date User Action Args
2020-09-03 00:33:50hauntsaninjasetrecipients: + hauntsaninja, asvetlov, yselivanov
2020-09-03 00:33:50hauntsaninjasetmessageid: <[email protected]>
2020-09-03 00:33:50hauntsaninjalinkissue41696 messages
2020-09-03 00:33:50hauntsaninjacreate