0

I have looked at previous answers and none of them seem to solve my issue. I am running the below code

from pyracing.client import Client
import asyncio

username = 'My Email Address'
password = 'My Password'

# Authentication is automated and will be initiated on first request
ir = Client(username, password)

# Example async function with hardcoded results
async def main():

seasons_list = await ir.current_seasons()

for season in seasons_list:
    if season.season_id == 2846:
        print(f'Schedule for {season.series_name_short}' 
                f' ({season.season_year} S{season.season_quarter})')

        for t in season.tracks:
            print(f'\tWeek {t.race_week} will take place at {t.name} ({t.config})')

asyncio.run(main())

and I get the below error:

RuntimeError                              Traceback (most recent call last)
Cell In[1], line 23
 20             for t in season.tracks:
 21                 print(f'\tWeek {t.race_week} will take place at {t.name}     ({t.config})')
---> 23 asyncio.run(main())

File ~\AppData\Local\anaconda3\Lib\asyncio\runners.py:191, in run(main, debug,     loop_factory)
161 """Execute the coroutine and return the result.
162 
163 This function runs the passed coroutine, taking care of
(...)
187     asyncio.run(main())
188 """
189 if events._get_running_loop() is not None:
190     # fail fast with short traceback
--> 191     raise RuntimeError(
192         "asyncio.run() cannot be called from a running event loop")
194 with Runner(debug=debug, loop_factory=loop_factory) as runner:
195     return runner.run(main)

RuntimeError: asyncio.run() cannot be called from a running event loop

what is am trying to eliminate is teh runtime error issue

3
  • 1
    Can't reproduce. Is this on something like Jupyter and you have a running loop in the kernel already? Commented Aug 14 at 10:24
  • 1
    you have wrong indentation. All code is outside main(). Always put correctly formatted code. Commented Aug 14 at 11:54
  • The error in my case occurs executing in local, not Jupyter Commented Oct 24 at 8:34

1 Answer 1

0

From your logs I assume you're running this in Jupyter Cell In[1].

So yeah in that case this error message explains itself:
RuntimeError: asyncio.run() cannot be called from a running event loop

This is due to Jupyter already having an event loop running, so calling asyncio.run() fails because it tries to start a new loop.

Here is what you can do :

  • First before all, please fix your identation.

  • Switch to plain python script so you can use asyncio.run()

  • Stay in Jupyter and simply use
    await main() instead of asyncio.run(main())

Sign up to request clarification or add additional context in comments.

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.