XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet
公开
关注 0 Fork 0 Star 1
返回提交历史

XFEstudio/gpt4free

fix(async): add Python 3.8 compatible to_thread helper for async cookie and worker execution

be37fcd3
Anand Mall <anand@example.com>
提交于

代码差异

4 个文件 +18 -4
Modified g4f/Provider/qwen/cookie_generator.py +3 -1
@@ -400,7 +400,9 @@ async def refresh_cookies():
400 400 global _current_cookies
401 401 try:
402 402 # generate_cookies() is CPU-bound sync; run it off the event loop.
403 result = await asyncio.to_thread(generate_cookies)
403 from ...providers.asyncio import to_thread
404
405 result = await to_thread(generate_cookies)
404 406
405 407 async with _lock:
406 408 _current_cookies = {
Modified g4f/cli/client.py +3 -1
@@ -366,7 +366,9 @@ async def async_run_client_args(args, exit_on_error=True):
366 366 md = MarkItDown()
367 367 return md.convert_url(url).text_content
368 368
369 txt = await asyncio.to_thread(run_markitdown, tok)
369 from g4f.providers.asyncio import to_thread
370
371 txt = await to_thread(run_markitdown, tok)
370 372 input_txt += f"\n```source: {tok}\n{txt}\n```\n"
371 373 elif os.path.isfile(tok):
372 374 from g4f.image import is_accepted_format
Modified g4f/cookies.py +2 -2
@@ -155,8 +155,8 @@ async def get_cookies_async(
155 155 """Async helper to load cookies without blocking the event loop."""
156 156 if single_browser != "all" and domain_name in CookiesConfig.cookies:
157 157 return CookiesConfig.cookies[domain_name]
158 import asyncio
159 return await asyncio.to_thread(
158 from .providers.asyncio import to_thread
159 return await to_thread(
160 160 get_cookies, domain_name, raise_requirements_error, single_browser, cache_result
161 161 )
162 162
Modified g4f/providers/asyncio.py +10 -0
@@ -1,6 +1,7 @@
1 1 from __future__ import annotations
2 2
3 3 import asyncio
4 import functools
4 5 from asyncio import AbstractEventLoop, runners
5 6 from typing import Optional, Callable, AsyncIterator, Iterator
6 7
@@ -117,3 +118,12 @@ async def to_async_iterator(iterator) -> AsyncIterator:
117 118 else:
118 119 for item in iterator:
119 120 yield item
121
122
123 async def to_thread(func: Callable, /, *args, **kwargs):
124 """Run a synchronous callable in a separate thread (Python 3.8+ compatible)."""
125 if hasattr(asyncio, "to_thread"):
126 return await asyncio.to_thread(func, *args, **kwargs)
127 loop = asyncio.get_running_loop()
128 return await loop.run_in_executor(None, functools.partial(func, *args, **kwargs))
129