# New to asyncio: how to run tasks in an enviroment where an event loop already exists

**URL:** <https://discuss.python.org/t/new-to-asyncio-how-to-run-tasks-in-an-enviroment-where-an-event-loop-already-exists/28847>\
**Category:** Python Help\
**Created:** [July 3, 2023, 6:25am UTC](https://discuss.python.org/t/new-to-asyncio-how-to-run-tasks-in-an-enviroment-where-an-event-loop-already-exists/28847 "2023-07-03T06:25:36Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![lmaurelli](https://avatars.discourse-cdn.com/v4/letter/l/7ba0ec/32.png) [@lmaurelli](https://discuss.python.org/u/lmaurelli)\
**Post date:** [July 3, 2023, 6:25am UTC](https://discuss.python.org/t/new-to-asyncio-how-to-run-tasks-in-an-enviroment-where-an-event-loop-already-exists/28847/1 "2023-07-03T06:25:36Z")

</div>

As stated in the title, how can I test and run asyncio tasks in an envrioment where an event loop already exists, e.g. in Spyder?

I tried the suggested [nest-asyncio · PyPI](https://pypi.org/project/nest-asyncio/) package on the web, but it seems not to work for me.

I tried this pattern too, unsuccesfully (syntax error “yield outside function” when I run `await main()`)

```auto
async def something():
    await some_async_work()

async def main():
   await something()

if __name__ == ' __main__':
   await main()

```

Am I missing something?

---

<div class="post-metadata">

**Author:** ![kknechtel](https://avatars.discourse-cdn.com/v4/letter/k/e47c2d/32.png) [@kknechtel](https://discuss.python.org/u/kknechtel)\
**Post date:** [July 3, 2023, 12:55pm UTC](https://discuss.python.org/t/new-to-asyncio-how-to-run-tasks-in-an-enviroment-where-an-event-loop-already-exists/28847/2 "2023-07-03T12:55:51Z")

</div>

> [@lmaurelli](#):
>
> I tried this pattern too, unsuccesfully (syntax error “yield outside function” when I run `await main()`)
> 
> ``

This part, at least, has nothing to do with Spyder, and is just as easily reproduced at the REPL.  
Calling `main` creates a coroutine object, which needs to be “run” within an event loop - it can’t just be `await`ed at top level.

`asyncio.get_event_loop` should give you an appropriate main event loop - which, when using Spyder to test the code, should be (to my understanding) the one that it already set up.

Try:

```auto
import asyncio

async def something():
    await some_async_work()

async def main():
   await something()

if __name__ == ' __main__':
   asyncio.get_event_loop().run_until_complete(main())

```

---

<div class="post-metadata">

**Author:** ![lmaurelli](https://avatars.discourse-cdn.com/v4/letter/l/7ba0ec/32.png) [@lmaurelli](https://discuss.python.org/u/lmaurelli)\
**Post date:** [July 3, 2023, 1:02pm UTC](https://discuss.python.org/t/new-to-asyncio-how-to-run-tasks-in-an-enviroment-where-an-event-loop-already-exists/28847/3 "2023-07-03T13:02:43Z")

</div>

I’m learning `asyncio` these days though the Python documentation and its high-level API which aims not to bother with the loop event itself. All the examples are initiated through the call as in `asyncio.run(main())`. This results in an error in Spyder and other enviroment which I wish to resolve. The error is `This event loop is already running` and also your solution gives me this error. I don’t know if I am missing something. Does your snippet works in a enviroment where an “event loop” is already set-up?

---

<div class="post-metadata">

**Author:** ![Rosuav](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rosuav/32/3429_2.png) [@Rosuav](https://discuss.python.org/u/Rosuav)\
**Post date:** [July 3, 2023, 2:00pm UTC](https://discuss.python.org/t/new-to-asyncio-how-to-run-tasks-in-an-enviroment-where-an-event-loop-already-exists/28847/4 "2023-07-03T14:00:30Z")

</div>

> [@kknechtel](#):
>
> This part, at least, has nothing to do with Spyder, and is just as easily reproduced at the REPL.  
> Calling `main` creates a coroutine object, which needs to be “run” within an event loop - it can’t just be `await`ed at top level.

Side note: The asyncio REPL can handle top-level await. It’s just the regular REPL, which doesn’t have an event loop, that can’t.

---

<div class="post-metadata">

**Author:** ![lmaurelli](https://avatars.discourse-cdn.com/v4/letter/l/7ba0ec/32.png) [@lmaurelli](https://discuss.python.org/u/lmaurelli)\
**Post date:** [July 7, 2023, 7:40am UTC](https://discuss.python.org/t/new-to-asyncio-how-to-run-tasks-in-an-enviroment-where-an-event-loop-already-exists/28847/5 "2023-07-07T07:40:35Z")

</div>

I still have not found a solution to my problem, to date I am learning asyncio by running the Python interpreter from a terminal and not from an coding enviroment with an already set-up event loop. If someone has a solution, I will gladly try it, thanks.

---

<div class="post-metadata">

**Author:** ![Rosuav](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rosuav/32/3429_2.png) [@Rosuav](https://discuss.python.org/u/Rosuav)\
**Post date:** [July 7, 2023, 8:30am UTC](https://discuss.python.org/t/new-to-asyncio-how-to-run-tasks-in-an-enviroment-where-an-event-loop-already-exists/28847/6 "2023-07-07T08:30:34Z")

</div>

> [@lmaurelli](#):
>
> to date I am learning asyncio by running the Python interpreter from a terminal

Do you run this:

`python3`

Or this:

`python3 -m asyncio`

? The second one gives you a REPL with an event loop.

---

<div class="post-metadata">

**Author:** ![lmaurelli](https://avatars.discourse-cdn.com/v4/letter/l/7ba0ec/32.png) [@lmaurelli](https://discuss.python.org/u/lmaurelli)\
**Post date:** [July 7, 2023, 9:28am UTC](https://discuss.python.org/t/new-to-asyncio-how-to-run-tasks-in-an-enviroment-where-an-event-loop-already-exists/28847/7 "2023-07-07T09:28:43Z")

</div>

I run my script `main.py` as `python.exe -i main.py` on Windows

---

<div class="post-metadata">

**Author:** ![Rosuav](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rosuav/32/3429_2.png) [@Rosuav](https://discuss.python.org/u/Rosuav)\
**Post date:** [July 7, 2023, 9:33am UTC](https://discuss.python.org/t/new-to-asyncio-how-to-run-tasks-in-an-enviroment-where-an-event-loop-already-exists/28847/8 "2023-07-07T09:33:09Z")

</div>

> [@lmaurelli](#):
>
> python.exe -i main.py

Ah, so you only have the basic REPL, not an asyncio-aware one. I’d recommend trying out `python.exe -m asyncio` and then importing your script, to see how that works for you.

---

<div class="post-metadata">

**Author:** ![lmaurelli](https://avatars.discourse-cdn.com/v4/letter/l/7ba0ec/32.png) [@lmaurelli](https://discuss.python.org/u/lmaurelli)\
**Post date:** [July 7, 2023, 9:40am UTC](https://discuss.python.org/t/new-to-asyncio-how-to-run-tasks-in-an-enviroment-where-an-event-loop-already-exists/28847/9 "2023-07-07T09:40:02Z")

</div>

With the “basic REPL” it does work though, I do not have a problem when running the Python interpreter from a new terminal. The problem is when I launch my script in a enviroment as Spyder (which I would like to use). Does your suggestion still apply to me?
