Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cant run asyncio.run() from within the IDE #23568

Open
Nic-Roberts-Hud opened this issue Jan 27, 2025 · 1 comment
Open

Cant run asyncio.run() from within the IDE #23568

Nic-Roberts-Hud opened this issue Jan 27, 2025 · 1 comment

Comments

@Nic-Roberts-Hud
Copy link

Nic-Roberts-Hud commented Jan 27, 2025

Win 11 x64, Python 3.12, Spyder 5.5.1

I'm trying to run the device discovery code from the bleak Bluetooth library

import asyncio
from bleak import BleakScanner

async def main():
    devices = await BleakScanner.discover()
    for d in devices:
        print(d)

asyncio.run(main())

but keep getting the following error,

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

Full traceback,

Traceback (most recent call last):

  File ~\.conda\envs\BLE\Lib\site-packages\spyder_kernels\py3compat.py:356 in compat_exec
    exec(code, globals, locals)

  File c:\users\sengnr3\.spyder-py3\temp.py:18
    asyncio.run(main())

  File ~\.conda\envs\BLE\Lib\asyncio\runners.py:190 in run
    raise RuntimeError(

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

The code runs as expected when called from the command line.

@dalthviz
Copy link
Member

Hi @Nic-Roberts-Hud thank you for the report! I think if you are running IPython >=7 you should be able to run your code changing it to be something like:

from bleak import BleakScanner

async def main():
    devices = await BleakScanner.discover()
    for d in devices:
        print(d)

await main()

Did a quick check using Spyder 6.0.3 on Windows from our installer (which comes with IPython 8.30.0) and I'm able to run the following:

async def main():
    print("d")

await main()

Image

For more info you can check https://stackoverflow.com/a/55409674/15954282

Let us know if the info above helps!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants