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

AsyncSession does not work on Windows platforms. #70

Closed
MooshiMochi opened this issue Jun 27, 2023 · 7 comments
Closed

AsyncSession does not work on Windows platforms. #70

MooshiMochi opened this issue Jun 27, 2023 · 7 comments

Comments

@MooshiMochi
Copy link

import asyncio
from curl_cffi import requests

URL = "https://reaperscans.com/comics/2995-perfect-surgeon"


async def main():
    async with requests.AsyncSession(impersonate="chrome101") as s:
        r = await s.get(URL)
        print(r.text)
        print(r.status_code)


if __name__ == "__main__":

    asyncio.set_event_loop_policy(asyncio.WindowsProactorEventLoopPolicy())

    asyncio.run(main())

I ran this code. I have python 3.11.4 and curl_cffi 0.5.6 installed

This is the error I got:

Exception ignored from cffi callback <function socket_function at 0x000001F728227BA0>:
Traceback (most recent call last):
  File "C:\Users\rchir\OneDrive\Desktop\GitProjects\ManhwaUpdatesBot\.venv\Lib\site-packages\curl_cffi\aio.py", line 62, in socket_function
    loop.add_reader(sockfd, async_curl.process_data, sockfd, CURL_CSELECT_IN)
  File "C:\Users\rchir\AppData\Local\Programs\Python\Python311\Lib\asyncio\events.py", line 530, in add_reader
    raise NotImplementedError
NotImplementedError: 
Exception ignored from cffi callback <function socket_function at 0x000001F728227BA0>:
Traceback (most recent call last):
  File "C:\Users\rchir\OneDrive\Desktop\GitProjects\ManhwaUpdatesBot\.venv\Lib\site-packages\curl_cffi\aio.py", line 68, in socket_function
    async_curl._sockfds.remove(sockfd)
KeyError: 644
Exception ignored from cffi callback <function socket_function at 0x000001F728227BA0>:
Traceback (most recent call last):
  File "C:\Users\rchir\OneDrive\Desktop\GitProjects\ManhwaUpdatesBot\.venv\Lib\site-packages\curl_cffi\aio.py", line 65, in socket_function
    loop.add_writer(sockfd, async_curl.process_data, sockfd, CURL_CSELECT_OUT)
  File "C:\Users\rchir\AppData\Local\Programs\Python\Python311\Lib\asyncio\events.py", line 536, in add_writer
    raise NotImplementedError
NotImplementedError: 
Exception ignored from cffi callback <function socket_function at 0x000001F728227BA0>:
Traceback (most recent call last):
  File "C:\Users\rchir\OneDrive\Desktop\GitProjects\ManhwaUpdatesBot\.venv\Lib\site-packages\curl_cffi\aio.py", line 62, in socket_function
    loop.add_reader(sockfd, async_curl.process_data, sockfd, CURL_CSELECT_IN)
  File "C:\Users\rchir\AppData\Local\Programs\Python\Python311\Lib\asyncio\events.py", line 530, in add_reader
    raise NotImplementedError
NotImplementedError: 
@perklet
Copy link
Collaborator

perklet commented Jun 28, 2023

Accroding to the docs:

Changed in version 3.8: On Windows, ProactorEventLoop is now the default event loop.
The loop.add_reader() and loop.add_writer() methods are not supported.

You can use the SelectorEventLoop instead.

import sys, asyncio

if sys.version_info >= (3, 8) and sys.platform.lower().startswith("win"):
    asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())

@MooshiMochi
Copy link
Author

O, thanks, it worked!

@MooshiMochi
Copy link
Author

Think you should implement this into the library by default if possible tho!

@perklet perklet pinned this issue Jul 27, 2023
@T-256
Copy link
Contributor

T-256 commented Dec 12, 2023

Anyway to support ProactorEventLoop?
I found https://www.tornadoweb.org/en/latest/_modules/tornado/platform/asyncio.html patches
And example implementation at zeromq/pyzmq#1524
Can we have the same patch in curl-cffi?

@perklet
Copy link
Collaborator

perklet commented Dec 12, 2023

Cool, could you please submit a PR?

@T-256
Copy link
Contributor

T-256 commented Dec 12, 2023

Yes, I'll work on it.

@perklet
Copy link
Collaborator

perklet commented Feb 23, 2024

Thanks to @T-256, this is no longer an issue, no need to change loop since v0.6.0

@perklet perklet unpinned this issue Feb 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants