Python Forum

Full Version: bleak library RuntimeError: This event loop is already running
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

I would like to use Bleak library to read the datas from an Arduino nano 33 BLE sense.
I am a beginner so I am trying to do it step by step so that I understand what I do.
To start, I wanted to run the code from the library doc

import asyncio
from bleak import BleakScanner

async def run():
    async with BleakScanner() as scanner:
        await asyncio.sleep(5.0)
    for d in scanner.discovered_devices:
        print(d)

loop = asyncio.get_event_loop()
loop.run_until_complete(run())
And I get the error
Error:
Traceback (most recent call last): File "C:\Users\lemarqua\AppData\Local\Temp/ipykernel_7764/1465954928.py", line 1, in <module> runfile('C:/Users/lemarqua/Documents/Python Scripts/test_bleak.py', wdir='C:/Users/lemarqua/Documents/Python Scripts') File "C:\Users\lemarqua\Anaconda3\lib\site-packages\debugpy\_vendored\pydevd\_pydev_bundle\pydev_umd.py", line 167, in runfile execfile(filename, namespace) File "C:\Users\lemarqua\Anaconda3\lib\site-packages\debugpy\_vendored\pydevd\_pydev_imps\_pydev_execfile.py", line 25, in execfile exec(compile(contents + "\n", file, 'exec'), glob, loc) File "C:/Users/lemarqua/Documents/Python Scripts/test_bleak.py", line 18, in <module> loop.run_until_complete(run()) File "C:\Users\lemarqua\Anaconda3\lib\asyncio\base_events.py", line 592, in run_until_complete self._check_running() File "C:\Users\lemarqua\Anaconda3\lib\asyncio\base_events.py", line 552, in _check_running raise RuntimeError('This event loop is already running') RuntimeError: This event loop is already running
Then I tried to add nest_asyncio:
import asyncio
import nest_asyncio
nest_asyncio.apply()

from bleak import BleakScanner

async def run():
    async with BleakScanner() as scanner:
        await asyncio.sleep(5.0)
    for d in scanner.discovered_devices:
        print(d)

loop = asyncio.get_event_loop()
loop.run_until_complete(run())
But now, the script compile without ending or stopping.

I also tried after doing some research on asyncio :
import asyncio
#import nest_asyncio
#nest_asyncio.apply()

from bleak import BleakScanner

async def run():
    async with BleakScanner() as scanner:
        await asyncio.sleep(5.0)
    for d in scanner.discovered_devices:
        print(d)
        
def main():
    loop = asyncio.get_event_loop()
    loop.run_until_complete(run())
    loop.close()
    
main()
I get the same Runtime error

I have found some codes from which I would like to get inspired, but I said I am trying to do it step by step.
https://github.com/Ladvien/arduino_ble_s...ter/app.py
https://github.com/protostax/ProtoStax_A...Central.py

Thanks in advance,

Alice
Try to go to the website of the library
Then see if they recommend a python version,
If they do try to use that version.
(Sep-29-2021, 05:55 PM)Underscore Wrote: [ -> ]Try to go to the website of the library
Then see if they recommend a python version,
If they do try to use that version.

Thanks for the answer but I haven't seen any recommanded version of python, only mac/linux/windows os version
okay I looked into the issues on github and it seems that the problem is because I am using spyder

https://github.com/hbldh/bleak/issues/10