"进程池"中的"集合"对象在pymongo中不可调用错误

rok*_*rok 5 python multiprocessing pymongo

使用以下代码会导致:

'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.
Run Code Online (Sandbox Code Playgroud)

代码:来自多处理导入池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()
Run Code Online (Sandbox Code Playgroud)

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

完全追溯:

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.
Run Code Online (Sandbox Code Playgroud)

Ben*_*ier 1

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

为您提供两种解决方案:

  1. [:]在使用或进行线程化之前显式拉取文档list
  2. 提供光标apply_async并使用方法克隆光标clonehttp://api.mongodb.org/python/current/api/pymongo/cursor.html#pymongo.cursor.Cursor.clone