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

Asyncio example from docs causes "RuntimeError: Event loop is closed" on Python 3.8.5, 64bit Windows 10 Pro, works fine on Python 3.7.4 #1287

Closed
mborus opened this issue Sep 12, 2020 · 3 comments
Labels
duplicate This issue or pull request already exists

Comments

@mborus
Copy link

mborus commented Sep 12, 2020

Checklist

  • [X ] The bug is reproducible against the latest release and/or master.
  • [ X] There are no similar issues or pull requests to fix it yet.

Describe the bug

I'm trying the Asyncio example from the docs "https://www.python-httpx.org/async/"

import asyncio
import httpx

async def main():
    async with httpx.AsyncClient() as client:
        response = await client.get('https://www.example.com/')
        print(response)

asyncio.run(main())

To reproduce

  • Fresh install of Python 3.8.5 64bit on Windows 10 Pro 64bit
  • create virtual environment:
    py -3.8 -m pip venv venv
    venv\Scripts\activate
    python -m pip install httpx

save the example to "example.py" and run it

python example.py

Expected behavior

Output:
<Response [200 OK]>

Actual behavior

Output is printed, then RuntimeError

(venv) C:\temp\aa2>python example.py
<Response [200 OK]>
Exception ignored in: <function _ProactorBasePipeTransport.del at 0x000002C41F396040>
Traceback (most recent call last):
File "C:\Python38\lib\asyncio\proactor_events.py", line 116, in del
self.close()
File "C:\Python38\lib\asyncio\proactor_events.py", line 108, in close
self._loop.call_soon(self._call_connection_lost, None)
File "C:\Python38\lib\asyncio\base_events.py", line 719, in call_soon
self._check_closed()
File "C:\Python38\lib\asyncio\base_events.py", line 508, in _check_closed
raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed

Environment

  • OS: Windows 10 Pro
  • Python version: 3.8.5 64bit
  • HTTPX version:

Name: httpx
Version: 0.14.3
Summary: The next generation HTTP client.
Home-page: https://github.com/encode/httpx
Author: Tom Christie
Author-email: tom@tomchristie.com
License: BSD
Location: c:\temp\aa2\venv2\lib\site-packages
Requires: httpcore, sniffio, chardet, rfc3986, certifi
Required-by:

  • Async environment:
    asyncio, not tried on trio, because this is the asyncio example

  • HTTP proxy: no

  • Custom certificates: no

Additional context

Error also happens when I try in PyCharm.

@florimondmanca
Copy link
Member

Heya! Looks like we already have an issue tracking this: #914

Going to close this off as a duplicate, feel free to go through that issue and add anything else that could be relevant!

FYI this seems to be an issue with the Proactor event loop in particular — there's a workaround here that suggests switching to SelectorEventLoop (i.e. what was used by default before 3.8): #914 (comment)

Thanks!

@florimondmanca florimondmanca added the duplicate This issue or pull request already exists label Sep 12, 2020
@mborus
Copy link
Author

mborus commented Sep 14, 2020

OK with the duplicate closing, but since the original issue request is quite old,
will you consider adding a warning to "https://www.python-httpx.org/async/"
below the first aynch example that the code currently doesn't work on Windows 3.8?

At the moment you may lose windows users that want to "switch" from requests to
httpx. My main reason to switch is to get asynch suport, to use it with FastAPI, so
if the hello-world-style example isn't working that's not good.

@florimondmanca
Copy link
Member

Yes, I suppose se could accept a PR adding a note or similar for Windows users, as a heads up that we're aware of this. :)

Though from my understanding our async support is compatible on Windows, just that there's an issue with the default Proactor event loop in 3.8 that causes a weird error on interpreter exit. So we can temporarily recommend that users use the pre-3.8 event loop if they want to drop the error, until that gets resolved either by us or Python. (Think I got this right?)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

2 participants