-
-
Notifications
You must be signed in to change notification settings - Fork 30.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
deprecate *loop* argument for asyncio.sleep #78909
Comments
asyncio.sleep is a coroutine; passing a *loop* argument to it makes no sense anymore. We should raise a DeprecationWarning in Python 3.8 and 3.9 and remove it in 4.0. |
Same for asyncio.wait and asyncio.wait_for. |
Tests are failed when ran with -Werror. $ ./python -Werror -m test -vuall test_asyncgen
... ====================================================================== Traceback (most recent call last):
File "/home/serhiy/py/cpython/Lib/test/test_asyncgen.py", line 404, in test_async_gen_asyncio_01
res = self.loop.run_until_complete(self.to_list(gen()))
File "/home/serhiy/py/cpython/Lib/asyncio/base_events.py", line 582, in run_until_complete
return future.result()
File "/home/serhiy/py/cpython/Lib/test/test_asyncgen.py", line 391, in to_list
async for i in gen:
File "/home/serhiy/py/cpython/Lib/test/test_asyncgen.py", line 398, in gen
await asyncio.sleep(0.01, loop=self.loop)
File "/home/serhiy/py/cpython/Lib/asyncio/tasks.py", line 598, in sleep
warnings.warn("The loop argument is deprecated and scheduled for "
DeprecationWarning: The loop argument is deprecated and scheduled for removal in Python 3.10. ====================================================================== $ ./python -Werror -m test -vuall test_asyncio
... ====================================================================== Traceback (most recent call last):
File "/home/serhiy/py/cpython/Lib/test/test_asyncio/utils.py", line 510, in close_loop
loop.close()
File "/home/serhiy/py/cpython/Lib/test/test_asyncio/utils.py", line 362, in close
self._gen.send(0)
File "/home/serhiy/py/cpython/Lib/test/test_asyncio/test_tasks.py", line 1363, in gen
self.assertAlmostEqual(10.0, when)
AssertionError: 10.0 != 0 within 7 places (10.0 difference) ====================================================================== Traceback (most recent call last):
File "/home/serhiy/py/cpython/Lib/test/test_asyncio/test_tasks.py", line 3189, in test_run_coroutine_threadsafe_task_factory_exception
self.assertEqual(len(callback.call_args_list), 1)
AssertionError: 2 != 1 ... |
On python-committers, it has been said that 3.10 will follow Python 3.9, no? |
Victor, you're looking at an outdated comment on this issue. This is what's in the master branch: Lines 598 to 599 in d4c76d9
|
No, I read the commit: warnings.warn("The loop argument is deprecated and scheduled for"
"removal in Python 4.0.",
DeprecationWarning, stacklevel=2)
Lines 598 to 599 in d4c76d9
Ah, you forgot to mention this bpo in your commit: commit fad6af2
Anyway, the current code is fine. Thanks. |
There is a new PR, so I change the issue resolution again. |
FYI if I recall correctly, in the past, we preferred to pass explicitly the loop to avoid to have to get the current loop which may add an overhead. But the current trend is to get rid of the explicit loop parameter.
sleep() requires the current event loop: if loop is None:
loop = events.get_running_loop()
else:
warnings.warn("The loop argument is deprecated and scheduled for "
"removal in Python 3.10.",
DeprecationWarning, stacklevel=2)
future = loop.create_future()
h = loop.call_later(delay,
futures._set_result_unless_cancelled,
future, result) Why does it not make sense to pass the loop to sleep? "it makes no sense anymore" something changes? I'm not against the change, I'm just trying to understand the rationale for other changes :-) |
My understanding is:
Passing it explicitly can be considered as microoptimization but On another hand passing *non-current* loop is a serious error: nothing prevents to do it but the code just hangs. |
[victor]
[andrew] What Andrew said. Basically, it wasn't *ever* possible to pass a loop to sleep() that would be different from the loop that would run it, because sleep() is a *coroutine*. In asyncio some APIs are functions and some are coroutines.
Passing the loop isn't even a viable micro-optimization, it's just pointless. This extra argument just adds to the confusion and promotes bad patterns, so we want to eventually remove it. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: