diff options
author | Heiner Lohaus <hlohaus@users.noreply.github.com> | 2024-01-14 16:38:59 +0100 |
---|---|---|
committer | Heiner Lohaus <hlohaus@users.noreply.github.com> | 2024-01-14 16:38:59 +0100 |
commit | 66576b39414c39e890109a523f07840d0c8fb17f (patch) | |
tree | 98959dd21460dbf910ba6454d1aada10248b5b1c | |
parent | Remove deprecation in get_event_loop (diff) | |
download | gpt4free-66576b39414c39e890109a523f07840d0c8fb17f.tar gpt4free-66576b39414c39e890109a523f07840d0c8fb17f.tar.gz gpt4free-66576b39414c39e890109a523f07840d0c8fb17f.tar.bz2 gpt4free-66576b39414c39e890109a523f07840d0c8fb17f.tar.lz gpt4free-66576b39414c39e890109a523f07840d0c8fb17f.tar.xz gpt4free-66576b39414c39e890109a523f07840d0c8fb17f.tar.zst gpt4free-66576b39414c39e890109a523f07840d0c8fb17f.zip |
-rw-r--r-- | g4f/Provider/helper.py | 14 |
1 files changed, 10 insertions, 4 deletions
diff --git a/g4f/Provider/helper.py b/g4f/Provider/helper.py index 160074f7..fce1ee6f 100644 --- a/g4f/Provider/helper.py +++ b/g4f/Provider/helper.py @@ -5,7 +5,7 @@ import os import random import secrets import string -from asyncio import AbstractEventLoop +from asyncio import AbstractEventLoop, BaseEventLoop from platformdirs import user_config_dir from browser_cookie3 import ( chrome, chromium, opera, opera_gx, @@ -27,13 +27,19 @@ def get_event_loop() -> AbstractEventLoop: AbstractEventLoop: The current or new event loop. """ try: - loop = asyncio.get_running_loop() + loop = asyncio.get_event_loop() + if isinstance(loop, BaseEventLoop): + loop._check_closed() + except RuntimeError: + loop = asyncio.new_event_loop() + asyncio.set_event_loop(loop) + try: + asyncio.get_running_loop() if not hasattr(loop.__class__, "_nest_patched"): import nest_asyncio nest_asyncio.apply(loop) except RuntimeError: - loop = asyncio.new_event_loop() - asyncio.set_event_loop(loop) + pass except ImportError: raise RuntimeError( 'Use "create_async" instead of "create" function in a running event loop. Or install "nest_asyncio" package.' |