Skip to content

Commit

Permalink
Merge pull request #1473 from xtekky/h
Browse files Browse the repository at this point in the history
Remove deprecation in get_event_loop
  • Loading branch information
hlohaus authored Jan 14, 2024
2 parents 42709f5 + 8472310 commit 32f49bc
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions g4f/Provider/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
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,
Expand All @@ -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.'
Expand Down

0 comments on commit 32f49bc

Please sign in to comment.