gpt4 book ai didi

python - 'Collection' object is not callable error in pymongo with process Pool

转载 作者:太空狗 更新时间:2023-10-30 01:34:05 24 4
gpt4 key购买 nike

使用下面的代码会导致:

'Collection' object is not callable. If you meant to call the '__getnewargs__' method on a 'Collection' object it is failing because no such method exists.

代码: 来自多处理导入池 db = MongoClient(ip,port)

def f(cursor, arg):
for doc in cursor:
...

p = Pool(4)
for arg in args:
cursor = db[dbName][collName].find()
p.apply_async(f,[cursor, arg])

db.close()

无法弄清楚问题是什么以及如何调试代码。

完整回溯:

Exception in thread Thread-2:
Traceback (most recent call last):
File "C:\Python27\lib\threading.py", line 808, in __bootstrap_inner
self.run()
File "C:\Python27\lib\threading.py", line 761, in run
self.__target(*self.__args, **self.__kwargs)
File "C:\Python27\lib\multiprocessing\pool.py", line 342, in _handle_tasks
put(task)
File "C:\Python27\lib\site-packages\pymongo\collection.py", line 1489, in __call__
self.__name.split(".")[-1])
TypeError: 'Collection' object is not callable. If you meant to call the '__getnewargs__' method on a 'Collection' object it is failing because no such method exists.

最佳答案

您在使用 cursor 时遇到问题。Collection.find 方法返回一个 Cursor 对象,它是一个消耗品。 ( http://api.mongodb.org/python/current/api/pymongo/cursor.html#pymongo.cursor.Cursor.getitem ) 我不知道这是否是异常的原因,但这肯定是一个问题。

两种解决方案:

  1. 在使用 [:]list
  2. 线程之前明确提取文档
  3. apply_async 中提供光标并使用 clone 方法克隆光标 (http://api.mongodb.org/python/current/api/pymongo/cursor.html#pymongo.cursor.Cursor.clone)

关于python - 'Collection' object is not callable error in pymongo with process Pool,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24655925/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com