前提条件:python程序中开启asyncio模型,进行异步处理
import asyncio
但是再程序中开启新线程,
threadQuery = threading.Thread(target=queryData, args=(word, queryId))
threadQuery.start()
线程函数是:
def queryData(word, queryId):
print("****************************************************************")
print("线程启动了!")
r_columns, r_bbox, r_labels, r_scores = cTry.connect_to_server_QUERY(word)
print("query结束了!")
print("****************************************************************")
做处理时,报错
RuntimeError: There is no current event loop in thread 'Thread-2'.
解决方案:
修改线程的函数,增加一个新的loop
def queryData(word, queryId):
print("****************************************************************")
print("线程启动了!")
print("word = ", word)
print("queryId = ", queryId)
# asyncio中启动线程,需要再开一个异步loop
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
r_columns, r_bbox, r_labels, r_scores = cTry.connect_to_server_QUERY(word)
print("query结束了!")
print("****************************************************************")