classification
Title: DeprecationWarning emitted when running asyncio tests
Type: Stage: resolved
Components: asyncio Versions: Python 3.11
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: asvetlov, erlendaasland, kumaraditya303, yselivanov
Priority: normal Keywords:

Created on 2022-01-11 05:06 by kumaraditya303, last changed 2022-01-22 11:26 by kumaraditya303. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 30486 merged kumaraditya303, 2022-01-11 05:06
Messages (6)
msg410279 - (view) Author: Erlend E. Aasland (erlendaasland) * (Python triager) Date: 2022-01-11 08:45
Providing some more background, based on the changes proposed by Kumar in GH-30486:

asyncio.get_event_loop() was marked as deprecated in Python 3.10. Quoting from the asyncio documentation , https://docs.python.org/3/library/asyncio-eventloop.html (as of 3.10):

    Because this function has rather complex behavior (especially when custom
    event loop policies are in use), using the get_running_loop() function is
    preferred to get_event_loop() in coroutines and callbacks.

    [...]

    Deprecated since version 3.10: Deprecation warning is emitted if there is
    no running event loop. In future Python releases, this function will be an
    alias of get_running_loop().


GH-30486 proposes to replace asyncio.get_event_loop() with asyncio.new_event_loop().
msg410280 - (view) Author: Erlend E. Aasland (erlendaasland) * (Python triager) Date: 2022-01-11 08:48
Kumar, did you consider using get_running_loop() to retrieve the event loop in the current thread, instead of creating a new one?
msg410281 - (view) Author: Erlend E. Aasland (erlendaasland) * (Python triager) Date: 2022-01-11 09:03
FTR, a rough grep for other users of get_event_loop in Lib/test (some false positives):

```
$ grep -rE "\<get_event_loop\>" Lib/test
Lib/test/test_contextlib_async.py:            loop = asyncio.get_event_loop_policy().get_event_loop()
Lib/test/test_asyncio/test_base_events.py:            'loop = asyncio.get_event_loop()',
Lib/test/test_asyncio/test_windows_events.py:        loop = asyncio.get_event_loop()
Lib/test/test_asyncio/test_runners.py:    def get_event_loop(self):
Lib/test/test_asyncio/test_runners.py:            loop = asyncio.get_event_loop()
Lib/test/test_asyncio/test_unix_events.py:            self.assertIsInstance(policy.get_event_loop(),
Lib/test/test_asyncio/test_unix_events.py:            policy.get_event_loop().close()
Lib/test/test_asyncio/test_unix_events.py:        loop = policy.get_event_loop()
Lib/test/test_asyncio/test_events.py:        self.assertRaises(NotImplementedError, policy.get_event_loop)
Lib/test/test_asyncio/test_events.py:        loop = policy.get_event_loop()
Lib/test/test_asyncio/test_events.py:        self.assertIs(loop, policy.get_event_loop())
Lib/test/test_asyncio/test_events.py:            loop = policy.get_event_loop()
Lib/test/test_asyncio/test_events.py:        self.assertRaises(RuntimeError, policy.get_event_loop)
Lib/test/test_asyncio/test_events.py:            self.assertRaises(RuntimeError, policy.get_event_loop)
Lib/test/test_asyncio/test_events.py:        old_loop = policy.get_event_loop()
Lib/test/test_asyncio/test_events.py:        self.assertIs(loop, policy.get_event_loop())
Lib/test/test_asyncio/test_events.py:        self.assertIsNot(old_loop, policy.get_event_loop())
Lib/test/test_asyncio/test_events.py:        self.get_event_loop_saved = events.get_event_loop
Lib/test/test_asyncio/test_events.py:        events.get_event_loop = type(self).get_event_loop_impl
Lib/test/test_asyncio/test_events.py:        asyncio.get_event_loop = type(self).get_event_loop_impl
Lib/test/test_asyncio/test_events.py:            events.get_event_loop = self.get_event_loop_saved
Lib/test/test_asyncio/test_events.py:            asyncio.get_event_loop = self.get_event_loop_saved
Lib/test/test_asyncio/test_events.py:            def get_event_loop(self):
Lib/test/test_asyncio/test_events.py:                    asyncio.get_event_loop()
Lib/test/test_asyncio/test_events.py:                    asyncio.get_event_loop()
Lib/test/test_asyncio/test_events.py:                self.assertIs(asyncio.get_event_loop(), loop)
Lib/test/test_asyncio/test_events.py:                    asyncio.get_event_loop()
Lib/test/test_asyncio/test_events.py:                    asyncio.get_event_loop()
Lib/test/test_asyncio/test_events.py:                loop2 = asyncio.get_event_loop()
Lib/test/test_asyncio/test_events.py:                    asyncio.get_event_loop()
Lib/test/test_asyncio/test_events.py:                self.assertIs(asyncio.get_event_loop(), loop)
Lib/test/test_asyncio/test_events.py:                self.assertIs(asyncio.get_event_loop(), loop)
Lib/test/test_asyncio/test_events.py:                    asyncio.get_event_loop()
```
msg410286 - (view) Author: Andrew Svetlov (asvetlov) * (Python committer) Date: 2022-01-11 10:47
get_event_loop() is deprecated when there is no running event loop.
Otherwise, it is equal to get_running_loop() and IS NOT deprecated.

I think there is no reason to replace all occurrences where get_event_loop() works fine.
msg410289 - (view) Author: Erlend E. Aasland (erlendaasland) * (Python triager) Date: 2022-01-11 11:00
> I think there is no reason to replace all occurrences where get_event_loop() works fine.

+1

I provided the grep for information only, hence FTR (for the record).
msg410290 - (view) Author: Erlend E. Aasland (erlendaasland) * (Python triager) Date: 2022-01-11 11:06
> get_event_loop() is deprecated when there is no running event loop.

Yes, I see that now. So using asyncio.new_event_loop instead makes sense.
History
Date User Action Args
2022-01-22 11:26:33kumaraditya303setstatus: open -> closed
resolution: fixed
stage: resolved
2022-01-11 11:06:45erlendaaslandsetmessages: + msg410290
2022-01-11 11:00:09erlendaaslandsetmessages: + msg410289
2022-01-11 10:47:11asvetlovsetmessages: + msg410286
2022-01-11 09:03:22erlendaaslandsetmessages: + msg410281
2022-01-11 08:48:38erlendaaslandsetmessages: + msg410280
2022-01-11 08:45:34erlendaaslandsetnosy: + erlendaasland
messages: + msg410279
2022-01-11 05:06:33kumaraditya303create