无法访问Celery Tasks中的Python实例变量

时间:2017-09-04 12:08:34

标签: python mongodb celery

我想在芹菜任务中进行MongoDB操作,但在访问实例变量时我遇到了一些问题。

继承代码

app = CeleryConnector().app

class CeleryTasks:
    def __init__(self, port, docID, dbName, collName, data):
        self.port = port
        self.docID = docID
        self.dbName = dbName
        self.collName = collName
        self.data = data
        self.client = MongoClient(host='localhost', port=port, username='admin', password='password')
        self.db = self.client[dbName]
        print ("CeleryTasks:init")


    @app.task
    def createDoc(dbName, collName, data):
        print ("CeleryTasks:CreateDoc")
        if 'refs' not in data:
            return

        # Here is the error I dont know how to access the client variable here.
        #db = client[dbName]
        print(data['refs'])

        for id in data['refs']:
            doc = db[collName].find_one({'_id': id})
            if doc is None:
                insertedID = db[collName].insert_one({
                    "_id": id
                })

                print (insertedID)

    @app.task(bind=True)
    def createDoc(self, dbName, collName, data):
        print ("CeleryTasks:CreateDoc")
        if 'refs' not in data:
            return

        print(data['refs'])

        for id in data['refs']:
            doc = self.db[collName].find_one({'_id': id})
            if doc is None:
                insertedID = self.db[collName].insert_one({
                    "_id": id
                })

                print (insertedID)

由于我们无法将非JSON可序列化对象传递给任务,因此传递 db或client 不是一种选择。

第一个功能出现问题 我不知道如何访问其中的客户端。试了几件但失败了。

第二个功能出现问题 它出错 doc = self.db [collName] .find_one({'_ id':id}) AttributeError:'createDoc'对象没有属性'db'  等等

如何使这项工作或如何访问芹菜任务中的实例变量?

0 个答案:

没有答案
相关问题