diff options
author | H Lohaus <hlohaus@users.noreply.github.com> | 2024-01-01 02:10:53 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-01-01 02:10:53 +0100 |
commit | e64a00332385d749d9c778f93421610ceb1eab54 (patch) | |
tree | 842b124b449f1741d2070b7d4981da0bd95bfd64 /g4f/Provider/helper.py | |
parent | Merge pull request #1413 from hlohaus/lia (diff) | |
parent | Patch event loop on win, Check event loop closed (diff) | |
download | gpt4free-e64a00332385d749d9c778f93421610ceb1eab54.tar gpt4free-e64a00332385d749d9c778f93421610ceb1eab54.tar.gz gpt4free-e64a00332385d749d9c778f93421610ceb1eab54.tar.bz2 gpt4free-e64a00332385d749d9c778f93421610ceb1eab54.tar.lz gpt4free-e64a00332385d749d9c778f93421610ceb1eab54.tar.xz gpt4free-e64a00332385d749d9c778f93421610ceb1eab54.tar.zst gpt4free-e64a00332385d749d9c778f93421610ceb1eab54.zip |
Diffstat (limited to 'g4f/Provider/helper.py')
-rw-r--r-- | g4f/Provider/helper.py | 38 |
1 files changed, 17 insertions, 21 deletions
diff --git a/g4f/Provider/helper.py b/g4f/Provider/helper.py index 61d9cb62..ded59ee2 100644 --- a/g4f/Provider/helper.py +++ b/g4f/Provider/helper.py @@ -7,9 +7,9 @@ import random import string import secrets import os -from os import path -from asyncio import AbstractEventLoop -from platformdirs import user_config_dir +from os import path +from asyncio import AbstractEventLoop +from platformdirs import user_config_dir from browser_cookie3 import ( chrome, chromium, @@ -25,37 +25,33 @@ from browser_cookie3 import ( from ..typing import Dict, Messages from .. import debug -# 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]] = {} -# If event loop is already running, handle nested event loops +# If loop closed or not set, create new event loop. +# If event loop is already running, handle nested event loops. # If "nest_asyncio" is installed, patch the event loop. def get_event_loop() -> AbstractEventLoop: try: - asyncio.get_running_loop() + loop = asyncio.get_event_loop() + loop._check_closed() except RuntimeError: - try: - return asyncio.get_event_loop() - except RuntimeError: - asyncio.set_event_loop(asyncio.new_event_loop()) - return asyncio.get_event_loop() + loop = asyncio.new_event_loop() + asyncio.set_event_loop(loop) try: - event_loop = asyncio.get_event_loop() - if not hasattr(event_loop.__class__, "_nest_patched"): + # Is running event loop + asyncio.get_running_loop() + if not hasattr(loop.__class__, "_nest_patched"): import nest_asyncio - nest_asyncio.apply(event_loop) - return event_loop + nest_asyncio.apply(loop) + except RuntimeError: + # No running event loop + pass except ImportError: raise RuntimeError( 'Use "create_async" instead of "create" function in a running event loop. Or install the "nest_asyncio" package.' ) + return loop def init_cookies(): urls = [ |