Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pymongo collection object serializing __getnewargs__ method

Pymongo collection object fails when i try to run my tasks with celery. I guess it couldn't serialize it. What should i do? By the way i tried pickle but it doesn't help me.

PS: I don't want to make global db import to this module.

'Collection' object is not callable. If you meant to call the 'getnewargs' method....

Sample code is below. module.py

from celery.contrib.methods import task_method



class Some_Class(object):
    """docstring for Some_Class"""
    def __init__(self, db):
        super(Some_Class, self).__init__()
        self.db = db  # This object causes 'Collection' object is not callable
        #db is a pymongo collection object from db.py


    @app.task(filter=task_method)  # Celery task
    def add(self):
        """
        db.insert({'some_key':'some_value'})
        """
        return 3

db.py

from pymongo import MongoClient    
db = MongoClient()['test']['collection']
like image 812
fatihsucu Avatar asked Dec 12 '14 17:12

fatihsucu


1 Answers

You should not be serializing collection objects. Instead store collection name and obtain collection object from within the task.

Collection objects require the associated established MongoClient object graph to be usable. Without the MongoClient, even if you somehow managed to deserialize a collection object, it wouldn't work.

like image 180
D. SM Avatar answered Sep 18 '22 06:09

D. SM