-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
Closed
Labels
Description
In the Coroutines and Tasks, there is a possible semantic bug in one of sample codes.
In the Awaitables subsection, the sample code for coroutines is:
import asyncio
async def nested():
return 42
async def main():
# Nothing happens if we just call "nested()".
# A coroutine object is created but not awaited,
# so it *won't run at all*.
nested()
# Let's do it differently now and await it:
print(await nested()) # will print "42".
asyncio.run(main())The code and its comments are all correct. But think about these two lines:
- in line 10:
nested() - in line 13:
print(await nested())
Since nested function doesn't print anything and just returns an integer number, if code in line 10 has no bug at all (even related to asyncio/async/await things), it prints nothing.
So I think, it is better to move the print to nested function and change the code sample to:
import asyncio
async def nested():
print(42)
async def main():
# Nothing happens if we just call "nested()".
# A coroutine object is created but not awaited,
# so it *won't run at all*.
nested() # will raise RuntimeWarning: coroutine 'nested' was never awaited
# Let's do it differently now and await it:
await nested() # will print "42".
asyncio.run(main())Linked PRs
Metadata
Metadata
Assignees
Labels
Projects
Status
Done