已解决 RuntimeError: There is no current event loop in thread ‘Thread-1‘.

简介: Jetson Xavier NX 报错 RuntimeError: There is no current event loop in thread 'Thread-1'.异常错误,已解决

Jetson Xavier NX报错异常RuntimeError: There is no current event loop in thread 'Thread-1'. 已解决


一、问题描述
这个错误是我在Jetson Xavier NX使用多线程的时候遇到的,这个异常是在线程 'Thread-1' 中没有当前的事件循环引起的。意思是使用 asyncio 库的时候,它就需要在主线程中创建一个事件循环并在这个事件循环上运行异步任务。

二、解决方法
解决方法其实很简单,只需要在主线程中创建和设置事件循环就行。


import asyncio
asyncio.set_event_loop(asyncio.new_event_loop())
    把上面的代码放到主线程里的最开始处。

    为啥将它限制在主线程的最开始处,主要原因就是避免在其他线程中运行涉及到 asyncio 的代码。将涉及到 asyncio 的代码移动到主线程里面,这样是确保有一个可用的事件循环。也意味你的异步任务会被限制在主线程里面去执行,而不是在其他线程里面执行。

image.png
image.png

目录
相关文章
|
6月前
|
NoSQL 编译器 API
关于thread使用的错误:pure virtual method called terminate called without an active exception
关于thread使用的错误:pure virtual method called terminate called without an active exception
127 1
|
27天前
|
传感器 JavaScript 前端开发
Event Loop
【10月更文挑战第29天】
32 4
|
5月前
|
Python
【ERROR】asyncio.run(main())报错:RuntimeError: Event loop is closed
【ERROR】asyncio.run(main())报错:RuntimeError: Event loop is closed
128 0
|
11月前
|
前端开发
The following tasks did not complete: first Did you forget to signal async completion?
The following tasks did not complete: first Did you forget to signal async completion?
|
JavaScript
event loop的理解
event loop的理解
Could not obtain transaction-synchronized Session for current thread原因及解决方案
Could not obtain transaction-synchronized Session for current thread原因及解决方案
282 1
Could not obtain transaction-synchronized Session for current thread原因及解决方案
Error:svn:E155037:Previous operation has not finished; run ‘cleanup‘ if it was interrupted(完美解决)
Error:svn:E155037:Previous operation has not finished; run ‘cleanup‘ if it was interrupted(完美解决)
419 0
Error:svn:E155037:Previous operation has not finished; run ‘cleanup‘ if it was interrupted(完美解决)
|
调度 C++
Thread.sleep(0) vs Thread.sleep(1) vs Thread.yield() vs Object.wait()
Thread.sleep(0) vs Thread.sleep(1) vs Thread.yield() vs Object.wait()
|
Python
may have been in progress in another thread when fork() was called.
may have been in progress in another thread when fork() was called.
140 0
may have been in progress in another thread when fork() was called.
|
Java 程序员 调度
Thread State 详解
前言 文本已收录至我的GitHub仓库,欢迎Star:github.com/bin39232820… 种一棵树最好的时间是十年前,其次是现在
168 0