3
import robloxapi, asyncio
client = robloxapi.Client(".ROBLOSECURITY Cookie Here") # Removed this for security reasons

async def main():
    user = await client.get_self()

    try:
        role = await user.get_role_in_group(1)
    except robloxapi.utils.errors.NotFound:
        role = None

    if role:    
        print(role.name)
    else:
        print("Not in group")

asyncio.run(main())

This code is raising RuntimeError: Event loop is closed and I don't have a clue why,

I have tried replacing asyncio.run with this

loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.close()

but it gave me the same error

Filip
  • 156
  • 1
  • 2
  • 9
  • Possible duplicate? https://stackoverflow.com/questions/45600579/asyncio-event-loop-is-closed . I haven't flagged – Thomas May 01 '20 at 13:31
  • Nope, that doesn't seem to change anything I'm still having the same error, also, In the comments they mentioned that it has that behavior by default when you use asyncio.run – Filip May 01 '20 at 14:06
  • I ended up running it in WSL and the exact same code worked – Filip May 02 '20 at 16:24
  • @Filip, i am making a writing a general answer about proactor en selector event loops to help some people out here. I was wondering if my answer helped you out? Could you give some feedback? – Gerrit Geeraerts Jun 16 '20 at 17:53

3 Answers3

18

On Windows do this:

asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
asyncio.run(main())
j4hangir
  • 2,532
  • 1
  • 13
  • 14
  • 1
    Thanks! I was getting angry because I wrote a correct code but constantly received this annoying error. So simple solution :). – Soren V. Raben Mar 29 '22 at 21:33
  • This worked for me. For those of you wondering [why](https://docs.python.org/3/library/asyncio-policy.html?highlight=asyncio%20set_event_loop_policy#asyncio.WindowsSelectorEventLoopPolicy). – SeaDude Oct 03 '22 at 03:07
3

I am new to StackOverflow and i can not write comments. So i will write it here as an answer. I ran into a similar problem with asyncio.

So the issue i had was solved by changing the Eventloop python was using.

In python versions lower than 3.8-:

  • SelectorEventLoop is used on windows
  • ProactorEventLoop is used on Linux.

(in python 3.8+ they are both ProactorEventLoop) So this will not help u out if u have python 3.8+ installed. As u will have the same eventloop on windows as on WSL.

if you do have a python version lower than 3.8- This will probably help you.

So you can try to manualy set the ProactorEventLoop wich is also used when u are using WSL

asyncio.set_event_loop(asyncio.ProactorEventLoop())

loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.close()

I hope this information will help you.

Gerrit Geeraerts
  • 924
  • 1
  • 7
  • 14
0

Ok I found out how to do this recently because I was having HELLA trouble with this problem. I found this answer on here and none of these worked for me. This post about the RuntimeError: Event loop closed error from PythonAlgos explains it really well. Here's the code if you just want to copy and paste:

from functools import wraps


from asyncio.proactor_events import _ProactorBasePipeTransport
 
def silence_event_loop_closed(func):
    @wraps(func)
    def wrapper(self, *args, **kwargs):
        try:
            return func(self, *args, **kwargs)
        except RuntimeError as e:
            if str(e) != 'Event loop is closed':
                raise
    return wrapper
 
_ProactorBasePipeTransport.__del__ = silence_event_loop_closed(_ProactorBasePipeTransport.__del__)
bujian
  • 152
  • 8