Skip to content

DeprecationWarning "There is no current event loop" from use of asyncio.get_event_loop #491

@mdickinson

Description

@mdickinson

The use of asyncio.get_event_loop causes a DeprecationWarning for Python 3.10, and will become an error in some future version of Python.

Here's the output of the test run on my machine on Python 3.10:

(traits-futures) mdickinson@mirzakhani traits-futures % python -m unittest
/Users/mdickinson/Enthought/Projects/traits-futures/traits_futures/asyncio/event_loop.py:28: DeprecationWarning: There is no current event loop
  self._event_loop = asyncio.get_event_loop()
................ssssssssssssssss............................................................/Users/mdickinson/.venvs/traits-futures/lib/python3.10/site-packages/pyface/base_toolkit.py:285: DeprecationWarning: SelectableGroups dict interface is deprecated. Use select.
  plugin for plugin in importlib_metadata.entry_points()[entry_point]
../Users/mdickinson/.venvs/traits-futures/lib/python3.10/site-packages/pyface/base_toolkit.py:216: DeprecationWarning: SelectableGroups dict interface is deprecated. Use select.
  entry_point_group = importlib_metadata.entry_points()[entry_point]
....ss................................................................................................/Users/mdickinson/Enthought/Projects/traits-futures/traits_futures/asyncio/event_loop.py:28: DeprecationWarning: There is no current event loop
  self._event_loop = asyncio.get_event_loop()
../Users/mdickinson/Enthought/Projects/traits-futures/traits_futures/asyncio/event_loop.py:28: DeprecationWarning: There is no current event loop
  self._event_loop = asyncio.get_event_loop()
./Users/mdickinson/Enthought/Projects/traits-futures/traits_futures/asyncio/event_loop.py:28: DeprecationWarning: There is no current event loop
  self._event_loop = asyncio.get_event_loop()
............/Users/mdickinson/Enthought/Projects/traits-futures/traits_futures/asyncio/event_loop.py:28: DeprecationWarning: There is no current event loop
  self._event_loop = asyncio.get_event_loop()
..................../Users/mdickinson/Enthought/Projects/traits-futures/traits_futures/asyncio/event_loop.py:28: DeprecationWarning: There is no current event loop
  self._event_loop = asyncio.get_event_loop()
../Users/mdickinson/Enthought/Projects/traits-futures/traits_futures/asyncio/event_loop.py:28: DeprecationWarning: There is no current event loop
  self._event_loop = asyncio.get_event_loop()
./Users/mdickinson/Enthought/Projects/traits-futures/traits_futures/asyncio/event_loop.py:28: DeprecationWarning: There is no current event loop
  self._event_loop = asyncio.get_event_loop()
..................../Users/mdickinson/Enthought/Projects/traits-futures/traits_futures/asyncio/event_loop.py:28: DeprecationWarning: There is no current event loop
  self._event_loop = asyncio.get_event_loop()
../Users/mdickinson/Enthought/Projects/traits-futures/traits_futures/asyncio/event_loop.py:28: DeprecationWarning: There is no current event loop
  self._event_loop = asyncio.get_event_loop()
./Users/mdickinson/Enthought/Projects/traits-futures/traits_futures/asyncio/event_loop.py:28: DeprecationWarning: There is no current event loop
  self._event_loop = asyncio.get_event_loop()
...........ssssssssssssssss
----------------------------------------------------------------------
Ran 284 tests in 19.763s

OK (skipped=34)

The easiest quick fix is to replace uses of asyncio.get_event_loop with asyncio.new_event_loop. The catch is that that leaves us responsible for clearing up (i.e., explicitly closing) those event loops, to avoid getting ResourceWarning warnings.

See https://bugs.python.org/issue39529 for the rationale for the deprecation, and related discussion.

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: refactorCleanup or refactoring tasks that don't add functionality or fix bugs

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions