Change event loop policy on windows

Support more versions from curl_cffi
This commit is contained in:
Heiner Lohaus 2023-10-01 20:29:57 +02:00
parent bb481a03ab
commit 7b9ad21de8
2 changed files with 30 additions and 12 deletions

View File

@ -1,13 +1,20 @@
from __future__ import annotations from __future__ import annotations
import asyncio import asyncio, sys
from asyncio import AbstractEventLoop from asyncio import AbstractEventLoop
import browser_cookie3 import browser_cookie3
# Change event loop policy on windows
if sys.platform == 'win32':
if isinstance(
asyncio.get_event_loop_policy(), asyncio.WindowsProactorEventLoopPolicy
):
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
# Local Cookie Storage
_cookies: dict[str, dict[str, str]] = {} _cookies: dict[str, dict[str, str]] = {}
# If event loop is already running, handle nested event loops # If event loop is already running, handle nested event loops
# If "nest_asyncio" is installed, patch the event loop. # If "nest_asyncio" is installed, patch the event loop.
def get_event_loop() -> AbstractEventLoop: def get_event_loop() -> AbstractEventLoop:

View File

@ -8,10 +8,12 @@ from aiohttp.base_protocol import BaseProtocol
from curl_cffi.requests import AsyncSession as BaseSession from curl_cffi.requests import AsyncSession as BaseSession
from curl_cffi.requests import Response from curl_cffi.requests import Response
from curl_cffi import AsyncCurl
is_newer_0_5_9 = hasattr(AsyncCurl, "remove_handle") import curl_cffi
is_newer_0_5_8 = hasattr(BaseSession, "_set_cookies")
is_newer_0_5_8 = hasattr(BaseSession, "_set_cookies") or hasattr(curl_cffi.requests.Cookies, "get_cookies_for_curl")
is_newer_0_5_9 = hasattr(curl_cffi.AsyncCurl, "remove_handle")
is_newer_0_5_10 = hasattr(BaseSession, "release_curl")
class StreamResponse: class StreamResponse:
def __init__(self, inner: Response, content: StreamReader, request): def __init__(self, inner: Response, content: StreamReader, request):
@ -65,13 +67,22 @@ class StreamRequest:
async def __aenter__(self) -> StreamResponse: async def __aenter__(self) -> StreamResponse:
self.curl = await self.session.pop_curl() self.curl = await self.session.pop_curl()
self.enter = self.loop.create_future() self.enter = self.loop.create_future()
request, _, header_buffer = self.session._set_curl_options( if is_newer_0_5_10:
self.curl, request, _, header_buffer, _, _ = self.session._set_curl_options(
self.method, self.curl,
self.url, self.method,
content_callback=self.on_content, self.url,
**self.options content_callback=self.on_content,
) **self.options
)
else:
request, _, header_buffer = self.session._set_curl_options(
self.curl,
self.method,
self.url,
content_callback=self.on_content,
**self.options
)
if is_newer_0_5_9: if is_newer_0_5_9:
self.handle = self.session.acurl.add_handle(self.curl) self.handle = self.session.acurl.add_handle(self.curl)
else: else: