我一直在尝试让异步函数从 Python 中的电机数据库中获取数据,并获取与搜索对应的数据列表。这是获取数据和打印数据的函数:
async def do_find_by_run_name(run_name):
"""
:param run_name: the name of a the run being searched for (string)
:return: MotorCursor for run found
"""
cursor = db.data_collection.find({"run_name":run_name})
loop = asyncio.get_event_loop()
return cursor
async def print_arr(cursor):
for d in await cursor.to_list(length=2):
pprint.pprint(d)
我有一个 PyQt 插槽,我想用它在按下按钮时调用 find_by_run_name 函数。这是该插槽的代码:
@pyqtSlot()
def on_find_runs_button_clicked(self):
try:
new_loop = asyncio.get_event_loop()
d = new_loop.run_until_complete(server.do_find_by_run_name("default"))
print(d)
new_loop = asyncio.get_event_loop()
v = new_loop.run_until_complete(server.print_arr(d))
except Exception as err:
try:
raise TypeError("Again !?!")
except:
pass
traceback.print_exc()
当我按下与此插槽对应的按钮时,我在终端中看到以下内容:
AsyncIOMotorCursor(<pymongo.cursor.Cursor object at 0x06F51EB0>)
Traceback (most recent call last):
File "C:/Users/Rohan Doshi/Documents/websockets/server\GUI.py", line 93, in on_find_runs_button_clicked
v = new_loop.run_until_complete(server.print_arr(d))
File "C:\Users\Rohan Doshi\AppData\Local\Programs\Python\Python36-32\lib\asyncio\base_events.py", line 468, in run_until_complete
return future.result()
File "C:/Users/Rohan Doshi/Documents/websockets/server\server.py", line 130, in print_arr
for d in await cursor.to_list(length=2):
RuntimeError: Task <Task pending coro=<print_arr() running at C:/Users/Rohan Doshi/Documents/websockets/server\server.py:130> cb=[_run_until_complete_cb() at C:\Users\Rohan Doshi\AppData\Local\Programs\Python\Python36-32\lib\asyncio\base_events.py:177]> got Future <Future pending> attached to a different loop
这向我表明 do_find_by_run_name 函数运行正常,但运行 print_arr 函数存在问题。
为了解决此问题,do_find_run_name 已更改为:
async def do_find_by_run_name(run_name):
"""
:param run_name: the name of a the run being searched for (string)
:return: MotorCursor for run found
"""
cursor = db.data_collection.find({"run_name":run_name})
print(cursor)
for d in await cursor.to_list(length=2):
pprint.pprint(d)
我将我的 PyQt 插槽更改为:
@pyqtSlot()
def on_find_runs_button_clicked(self):
try:
new_loop = asyncio.get_event_loop()
future = asyncio.run_coroutine_threadsafe(
server.do_find_by_run_name("default"),
new_loop
)
assert future.result(timeout=10)
except Exception as err:
try:
raise TypeError("Again !?!")
except:
pass
traceback.print_exc()
进行此更改后,我看不到任何打印内容。似乎 do_find_run_name couroutine 从未执行过。