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

Bug fix & Code Lint: fix RuntimeError: no running event loop #217

Closed
wants to merge 1 commit into from

Conversation

JavieHush
Copy link
Contributor

@JavieHush JavieHush commented Nov 6, 2024

When trying to get a loop, we'll always get an error said RuntimeError: no running event loop. The code could run correctly before several commits, however, it somehow got a bug now...

The problematic code is as follows

def always_get_an_event_loop() -> asyncio.AbstractEventLoop:
    try:
        loop = asyncio.get_running_loop()
    except RuntimeError:
        logger.info("Creating a new event loop in main thread.")
        # loop = asyncio.new_event_loop()
        # asyncio.set_event_loop(loop)
        loop = asyncio.get_event_loop()
    return loop

to fix this bug, the code can be modified as follows

def always_get_an_event_loop() -> asyncio.AbstractEventLoop:
    try:
        loop = asyncio.get_running_loop()
    except RuntimeError:
        logger.info("Creating a new event loop in main thread.")
        loop = asyncio.new_event_loop()
        asyncio.set_event_loop(loop)
    return loop

This fix could solve issue #214
Plus, I format the code in the project using ruff-format

@JavieHush JavieHush closed this Nov 6, 2024
@wiltshirek
Copy link
Contributor

wiltshirek commented Nov 6, 2024

The Neo4J Async lib has an issue leveraging the existing loop but it can use get_event_loop(). I tested both networKx and Neo4J with the following:

def always_get_an_event_loop() -> asyncio.AbstractEventLoop:
    try:
        **loop = asyncio.get_event_loop()**. 
    except RuntimeError:
        logger.info("Creating a new event loop in main thread.")
        loop = asyncio.new_event_loop()
        asyncio.set_event_loop(loop)
    return loop
This seems to work for both cases.    I can do a quick pull for this.  

@wiltshirek
Copy link
Contributor

wiltshirek commented Nov 6, 2024

fixed for all use cases (NX/Neo) in: #221 . thanks for sussing this out @JavieHush

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants