python - 无法使用标准方法将 MotorCursor 对象转换为异步函数中的列表
问题描述
我一直在尝试让异步函数从 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 从未执行过。
解决方案
推荐阅读
- cordova - 科尔多瓦。无法关闭标准后退按钮
- android - 如何在最近的应用程序中显示一个应用程序的 2 个不同活动?
- php - 如何从表中的输入中保存日期时间数据
- c++ - C++在其他项目中使用共享库而不指定-I
- python - 尝试转换数据框中的项目。也许是 JSON
- ios - 我正在尝试处理用户身份验证状态更改并分配根视图控制器不起作用?
- ios - 如何将从服务器接收到的图像加载到 SpriteKit 游戏中?
- docker - kubernetes pods 无法与外界通信
- javascript - 使用 Javascript 使某些字段根据下拉值消失
- asp.net-core - 通过互联网公开我的 asp.net 核心网络应用程序