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

4
  • Possible duplicate? stackoverflow.com/questions/45600579/… . I haven't flagged Commented May 1, 2020 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 Commented May 1, 2020 at 14:06
  • I ended up running it in WSL and the exact same code worked Commented May 2, 2020 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? Commented Jun 16, 2020 at 17:53

3 Answers 3

18

On Windows do this:

asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
asyncio.run(main())
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! I was getting angry because I wrote a correct code but constantly received this annoying error. So simple solution :).
This worked for me. For those of you wondering why.
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.

Comments

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__)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.