我一直试图让async函数从Python中的Motor数据库获取数据并获取与搜索相对应的数据列表。这是获取数据和打印数据的功能:
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协程。