Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix for #209 #354

Merged
merged 1 commit into from
Dec 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions lightrag/lightrag.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,28 @@


def always_get_an_event_loop() -> asyncio.AbstractEventLoop:
"""
Ensure that there is always an event loop available.

This function tries to get the current event loop. If the current event loop is closed or does not exist,
it creates a new event loop and sets it as the current event loop.

Returns:
asyncio.AbstractEventLoop: The current or newly created event loop.
"""
try:
return asyncio.get_event_loop()
# Try to get the current event loop
current_loop = asyncio.get_event_loop()
if current_loop._closed:
raise RuntimeError("Event loop is closed.")
return current_loop

except RuntimeError:
# If no event loop exists or it is closed, create a new one
logger.info("Creating a new event loop in main thread.")
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)

return loop
new_loop = asyncio.new_event_loop()
asyncio.set_event_loop(new_loop)
return new_loop


@dataclass
Expand Down