简体   繁体   中英

I don't know how to fix the following error; RuntimeError: There is no current event loop in thread 'Thread-1' and 'Thread-2'

I'm a beginner at coding, and its my very first time using threads. Threads was the only solution I could find for my project. In the project I'm supposed to show on the app which sensor is vibrating, and to do that i drew 4 circles around to represent the sensor. So sensor 1 would be bytearray(b'\x03'), sensor 2 would be bytearray(b'\x04') and so on. What i would want is for the color of sensor 1 to change when the characteristic value is bytearray(b'\x03').

Currently using python 3.9 to create an app that connects through bluetooth with an arduino nano 33 iot, and i want to be able to read the characteristic every.3 second, and based on the value its supposed to change the color of a canvas drawing. My intention here is to have the while loop running in the background reading the characteristic value every.3 second. And on my main screen, based on the value that's read, for an example, if it reads bytearray(b'\x03') it supposed to change the color of one drawing on the screen. (That's why the if statement, so that condition to change color would replace the 'print("changed color")). Also there will be multiple sensors to check bytearray(b'\x03') through bytearray(b'\x10'), so i dont know if an if statement is the best to use, so if you have a better suggestion please let me know.

def background():
    async def run(address):
        async with BleakClient(address, loop=loop) as client:
            await client.get_services()
            while True:
                value = await client.read_gatt_char(r_characteristic)
                if await client.read_gatt_char(r_characteristic):
                    time.sleep(.3)
                    print("Read Value: {0}".format(value))

    loop = asyncio.get_event_loop()

def foreground():
    async def run(address):
        async with BleakClient(address, loop=loop) as client:
            await client.get_services()
            value = await client.read_gatt_char(r_characteristic)
            if await client.read_gatt_char(r_characteristic) == bytearray(b'\x03'):
                print("changed color")

    loop = asyncio.get_event_loop()
    asyncio.ensure_future(run(address))
    loop.run_forever()


b = threading.Thread(target=background)
f = threading.Thread(target=foreground)

b.start()
f.start()

However when i run it, it send me an error back as shown below;

raise RuntimeError('There is no current event loop in thread %r.'
     raise RuntimeError('There is no current event loop in thread %r.'    raise RuntimeError('There is no current event loop in thread %r.'
 RuntimeError: There is no current event loop in thread 'Thread-1'.
 RuntimeError: There is no current event loop in thread 'Thread-2'.

Any help will be very much appreciated!

Try adding these two lines at the end of your code:

b.join()
f.join()

Might work, might not work, but as Tim Roberts has correctly said, combining threads and async is rather unusual and you might need to refactor your code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM