Skip to content
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

DeprecationWarning: There is no current event loop #5685

Closed
wRAR opened this issue Oct 20, 2022 · 7 comments · Fixed by #5689
Closed

DeprecationWarning: There is no current event loop #5685

wRAR opened this issue Oct 20, 2022 · 7 comments · Fixed by #5689

Comments

@wRAR
Copy link
Member

wRAR commented Oct 20, 2022

/home/runner/work/scrapy/scrapy/scrapy/utils/reactor.py:70: DeprecationWarning: There is no current event loop
  event_loop = asyncio.get_event_loop()
tests/test_downloadermiddleware.py: 2 warnings
tests/test_utils_asyncgen.py: 2 warnings
tests/test_utils_defer.py: 4 warnings
tests/test_utils_python.py: 2 warnings
tests/test_utils_signal.py: 2 warnings
  /home/runner/work/scrapy/scrapy/scrapy/utils/defer.py:272: DeprecationWarning: There is no current event loop
    return Deferred.fromFuture(asyncio.ensure_future(o))
tests/test_utils_asyncio.py::AsyncioTest::test_install_asyncio_reactor
  /home/runner/work/scrapy/scrapy/scrapy/utils/reactor.py:70: DeprecationWarning: There is no current event loop
    event_loop = asyncio.get_event_loop()

Not sure yet what does this imply and when can this break (this is from tests on 3.10 and 3.11).

@wRAR
Copy link
Member Author

wRAR commented Oct 20, 2022

asyncio.get_event_loop() now emits a deprecation warning if there is no running event loop. In the future it will be an alias of get_running_loop(). asyncio functions which implicitly create Future or Task objects now emit a deprecation warning if there is no running event loop and no explicit loop argument is passed

@shashwata27
Copy link

can I add an exception handling to this then? @wRAR

@wRAR
Copy link
Member Author

wRAR commented Oct 21, 2022

@shashwata27 what kind of exception handling?

@Gallaecio
Copy link
Member

Gallaecio commented Oct 21, 2022

Maybe we can just switch to get_running_loop(), it has been around since Python 3.7, which is our minimum Python version now.

I wonder, though, if there is any case where we can get a RuntimeError when using it. Looking at the deprecation warnings, they explicitly say that there is no running event loop, which would trigger a RuntimeError with get_running_loop().

@Gallaecio
Copy link
Member

For:

event_loop = asyncio.get_event_loop()

A try-except like this may make sense:

try:
    event_loop = asyncio.get_running_loop()
except RuntimeError:
    event_loop = asyncio.new_event_loop()

@Gallaecio
Copy link
Member

This seems problematic, though:

return d.asFuture(asyncio.get_event_loop())

@Gallaecio
Copy link
Member

Gallaecio commented Oct 21, 2022

In the latter, maybe we could start by switching to get_running_loop(), and see what breaks. It should really only be called if there is a running loop, as far as I can tell. If not, maybe we are doing something wrong.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants