简体   繁体   English

我不知道如何解决以下错误; RuntimeError:线程“Thread-1”和“Thread-2”中没有当前事件循环

[英]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. Threads 是我能为我的项目找到的唯一解决方案。 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.在项目中,我应该在应用程序上显示哪个传感器正在振动,为此我画了 4 个圆圈来代表传感器。 So sensor 1 would be bytearray(b'\x03'), sensor 2 would be bytearray(b'\x04') and so on.所以传感器 1 将是 bytearray(b'\x03'),传感器 2 将是 bytearray(b'\x04') 等等。 What i would want is for the color of sensor 1 to change when the characteristic value is bytearray(b'\x03').我想要的是当特征值为 bytearray(b'\x03') 时传感器 1 的颜色发生变化。

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.目前使用 python 3.9 创建一个应用程序,该应用程序通过蓝牙与 arduino nano 33 iot 连接,我希望能够每 3 秒读取一次特性,并根据它应该改变 ZFCC790C72A86190DEB 绘图颜色的值。 My intention here is to have the while loop running in the background reading the characteristic value every.3 second.我的目的是让 while 循环在后台运行,每 3 秒读取一次特征值。 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.在我的主屏幕上,基于读取的值,例如,如果它读取 bytearray(b'\x03') 它应该改变屏幕上一个绘图的颜色。 (That's why the if statement, so that condition to change color would replace the 'print("changed color")). (这就是 if 语句的原因,因此更改颜色的条件将替换 '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.还会有多个传感器通过 bytearray(b'\x10') 来检查 bytearray(b'\x03'),所以我不知道 if 语句是否最好使用,所以如果您有更好的建议,请告诉我知道。

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.可能有效,也可能无效,但正如 Tim Roberts 正确所说,将线程和异步结合起来是相当不寻常的,您可能需要重构代码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 RuntimeError: 线程 'Thread-1' 中没有当前事件循环,多线程和异步错误 - RuntimeError: There is no current event loop in thread 'Thread-1' , multithreading and asyncio error Flask asyncio aiohttp - RuntimeError:线程'Thread-2'中没有当前事件循环 - Flask asyncio aiohttp - RuntimeError: There is no current event loop in thread 'Thread-2' /accounts/register/ 处的 RuntimeError 线程 'Thread-1' 中没有当前事件循环 - RuntimeError at /accounts/register/ There is no current event loop in thread 'Thread-1' “RuntimeError:线程 'Thread-2' 中没有当前事件循环。” 错误是什么意思? - what does a "RuntimeError: There is no current event loop in thread 'Thread-2'." error mean? python龙卷风websocket错误:RuntimeError:线程'Thread-1'中没有当前事件循环 - python tornado websocket error: RuntimeError: There is no current event loop in thread 'Thread-1' 线程 'Thread-1' 中没有当前事件循环 - There is no current event loop in thread 'Thread-1' 多线程 python 时的运行时错误“运行时错误:线程 'Thread-1' 中没有当前事件循环。” - RuntimeError when multithreading python "RuntimeError: There is no current event loop in thread 'Thread-1'." RuntimeError:线程“ Thread-1”中没有当前事件循环。 -request_html,html.render() - RuntimeError: There is no current event loop in thread 'Thread-1'. - requests_html, html.render() requests-html“RuntimeError:在 flask 端点上使用时,线程‘Thread-1’中没有当前事件循环 - requests-html "RuntimeError: There is no current event loop in thread 'Thread-1' when using it on a flask endpoint 将 Telethon 与 django 一起使用:线程 'Thread-1' 中没有当前事件循环 - Using telethon with django: There is no current event loop in thread 'Thread-1'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM