qwertyquerty / pypresence

A complete Discord IPC and Rich Presence wrapper library in Python!
https://qwertyquerty.github.io/pypresence/html/index.html
MIT License
652 stars 76 forks source link

Return ProactorEventLoop for windows #220

Closed brostosjoined closed 1 year ago

brostosjoined commented 1 year ago

In case where there is a running loop but the loop is not Proactor Event loop.

TheSpookyCat commented 1 year ago

The only reason this should happen is if someone has set a loop that doesn't support pipes as the default. Other loops are not guaranteed to be supported.

See: https://docs.python.org/3.6/library/asyncio-eventloops.html

On Windows, only sockets are supported (ex: pipes are not supported).

I'm happy to look at this PR again if the existing code causes problems but all of my tests show the function is stable.

brostosjoined commented 1 year ago

Here are some few modification

def get_event_loop(force_fresh=False):
    loop = asyncio.ProactorEventLoop() if sys.platform == 'win32' else asyncio.new_event_loop()
    if force_fresh:
        return loop
    try:
        running = asyncio.get_running_loop()
    except RuntimeError:
        return loop
    if running.is_closed():
        return loop
    else:
        if sys.platform in ('linux', 'darwin'):
            return running
        if sys.platform == 'win32':
            if isinstance(running, asyncio.ProactorEventLoop):
                return running
            else:
                return loop