Python Forum
bleak library RuntimeError: This event loop is already running
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
bleak library RuntimeError: This event loop is already running
#1
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
Reply
#2
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.
Reply
#3
(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
Reply
#4
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Read 2 Value with bleak via BLE notify from ESP32 Nietzsche 8 2,784 May-04-2023, 09:31 PM
Last Post: Nietzsche
  googletrans library to translate text language for using data frame is not running gcozba2023 0 1,223 Mar-06-2023, 09:50 AM
Last Post: gcozba2023
  How to continuously receive messages until specified to stop using Bleak jacobbreen25 3 2,121 Dec-28-2022, 04:25 PM
Last Post: jacobbreen25
  help RuntimeError: no running event loop marpaslight 5 3,713 Oct-18-2022, 10:04 PM
Last Post: marpaslight
  Help with bleak - how to send test to BLE device? korenron 1 1,715 Aug-28-2022, 11:28 PM
Last Post: Larz60+
  A question about 'Event loop is closed' fc5igm 2 2,202 Oct-05-2021, 02:00 AM
Last Post: fc5igm
  loop running indefinitely shantanu97 6 2,556 Sep-29-2021, 08:03 PM
Last Post: deanhystad
  Running A Loop Until You See A Particular Result knight2000 6 31,675 Sep-04-2021, 08:55 AM
Last Post: knight2000
  Running loop at specific frequency mdsousa 3 5,919 Apr-21-2021, 11:22 AM
Last Post: jefsummers
  RuntimeError: generator raised StopIteration quest 1 5,799 Mar-28-2021, 08:11 PM
Last Post: quest

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020