2014-07-09 2 views
5

den Code unten Ursachen:'Collection' Objekt ist nicht aufrufbar Fehler in pymongo mit Prozess Pool

'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. 

Der Code: von Multiprozessor-Import Pool 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() 

Kann nicht herausfinden, was das Problem ist und wie der Code debuggen.

Volltraceback:

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. 
+1

mit einem Gewinde Bei In welcher Zeile tritt der Fehler auf? –

+0

aktualisiert mit vollständigem Traceback – user1264304

+0

Wird 'Pool' vom' multiprocessing' Paket der obersten Ebene oder von einem anderen Modul (wie 'multiprocessing.dummy' oder etwas in pymongo) importiert? – Blckknght

Antwort

1

Sie haben ein Problem bei der Verwendung von cursor. Die Methode Collection.find gibt ein Objekt Cursor zurück, das ein Verbrauchsmaterial ist. (http://api.mongodb.org/python/current/api/pymongo/cursor.html#pymongo.cursor.Cursor.getitem) Ich weiß nicht, ob dies die Ursache der Ausnahme ist, aber es ist sicherlich ein Problem.

Zwei Lösungen für Sie:

  1. Explizit die Dokumente ziehen, bevor [:] oder list
  2. Geben Sie den Cursor in apply_async und klonen Sie den Cursor mit clone Methode (http://api.mongodb.org/python/current/api/pymongo/cursor.html#pymongo.cursor.Cursor.clone)
Verwandte Themen