summaryrefslogtreecommitdiffstats
path: root/g4f/Provider/helper.py
diff options
context:
space:
mode:
authorTekky <98614666+xtekky@users.noreply.github.com>2023-10-02 12:20:18 +0200
committerGitHub <noreply@github.com>2023-10-02 12:20:18 +0200
commitc0632c27411b5cc8a643e0bffd71537f4224473c (patch)
treed8a07c23a8172e26004560e4d6b08982f637e71c /g4f/Provider/helper.py
parent~ | v-0.1.4.2 `pip install -U g4f` (diff)
parentFix: There is no current event loop in thread (diff)
downloadgpt4free-c0632c27411b5cc8a643e0bffd71537f4224473c.tar
gpt4free-c0632c27411b5cc8a643e0bffd71537f4224473c.tar.gz
gpt4free-c0632c27411b5cc8a643e0bffd71537f4224473c.tar.bz2
gpt4free-c0632c27411b5cc8a643e0bffd71537f4224473c.tar.lz
gpt4free-c0632c27411b5cc8a643e0bffd71537f4224473c.tar.xz
gpt4free-c0632c27411b5cc8a643e0bffd71537f4224473c.tar.zst
gpt4free-c0632c27411b5cc8a643e0bffd71537f4224473c.zip
Diffstat (limited to 'g4f/Provider/helper.py')
-rw-r--r--g4f/Provider/helper.py25
1 files changed, 15 insertions, 10 deletions
diff --git a/g4f/Provider/helper.py b/g4f/Provider/helper.py
index e14ae65e..234cdaa1 100644
--- a/g4f/Provider/helper.py
+++ b/g4f/Provider/helper.py
@@ -1,28 +1,33 @@
from __future__ import annotations
-import asyncio
-import sys
+import asyncio, sys
from asyncio import AbstractEventLoop
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]] = {}
-# Use own event_loop_policy with a selector event loop on windows.
-if sys.platform == 'win32':
- _event_loop_policy = asyncio.WindowsSelectorEventLoopPolicy()
-else:
- _event_loop_policy = asyncio.get_event_loop_policy()
-
# 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()
except RuntimeError:
- return _event_loop_policy.get_event_loop()
+ try:
+ return asyncio.get_event_loop()
+ except RuntimeError:
+ asyncio.set_event_loop(asyncio.new_event_loop())
+ return asyncio.get_event_loop()
try:
- event_loop = _event_loop_policy.get_event_loop()
+ event_loop = asyncio.get_event_loop()
if not hasattr(event_loop.__class__, "_nest_patched"):
import nest_asyncio
nest_asyncio.apply(event_loop)