Picklingerror:无法挑选Request对象

时间:2014-07-29 07:40:54

标签: python celery pyramid pickle

我知道不可能挑选金字塔请求对象,但我似乎无法找到我发送Request对象的位置。

请考虑以下事项:

@task
def do_consignment_task(store, agent):
    print "GOTHERE IN TASK"
    s = sqlahelper.get_session()
    consign = store.gen_consignment()
    ca = Agents.by_id(store.consignment_agents_id)
    consign.consignment_agents_id = ca.id
    consign.consignment_teamleader_id = ca.ou[0].lead_agents_id
    consign.consignment_timestamp = func.now()
    consign.created_by_agent_id = agent.id
    consign.complete_stamp = func.now()
    consign.sims =  store.sims
    consign.status = "SUCCESS"
    print "GOT BEFORE LOOP " 
    for sim in store.sims:
        if sim in consign.sims:
            continue
        else:
            consign.sims.append(sim)
    s.add(consign)
    transaction.savepoint()
    print "GOT AFTER SAVEPOINT"
    for sim in consign.sims:
        is_reconsign = sim.consignment_agent or sim.consignment_teamlead
        if is_reconsign:
            if not sim.consignment_history:
                sim.consignment_history = []
            sim.consignment_history.append(dict(
                stamp=sim.consignment_timestamp,
                consignment_agent_id=sim.consignment_agents_id,
                consignment_teamleader_id=sim.consignment_teamleader_id,
                by_agent_id=agent.id
            ))
        s.query(
            Sims
        ).filter(
            Sims.iccid == sim.iccid
        ).update(
            {
                "consignment_agents_id": consign.consignment_agents_id,
                "consignment_history": sim.consignment_history,
                "consignment_teamleader_id": ca.ou[0].lead_agents_id,
                "consignment_timestamp": func.now(),
                "modify_stamp": func.now(),
                "consignments_id": consign.id

            },
            synchronize_session=False
        )
        print "GOT BEFORE COMMIT"
        transaction.savepoint()
    print "THIS IS THE ID ID ID ID ID ID : ", consign.id

我将此功能称为:

if self.store.finalise:
    try:
        store = self.store
        agent = self.agent

        do_consignment_task.delay(store, agent)

        transaction.commit()
        self.check_and_purge()
        return "Consignmnet is being processed"
    except Exception, exc:
        self.check_and_purge()
        self.log.exception(exc)
        exc_error = "CONSIGNERR:", exc.message
        raise USSDFailure(exc_error)
else:
    self.store.status = "CANCELLED"
    if "fullconfirm" in self.session:
        del self.session["fullconfirm"]
    self.check_and_purge()
    return "CONSIGNMENT Cancelled"

当我运行此代码时,我收到以下错误:

EncodeError: Can't pickle <class 'pyramid.util.Request'>: attribute lookup pyramid.util.Request failed

我不发送selfrequest个对象 - 至少不是我能看到的。 怎么能解决这个问题?我发送了一个请求对象,因为我看不到一个?

可以看到追溯here

修改

好吧所以我试图更改我发送给函数的数据 - 我没有传递sqlalchemy对象,我正在复制store对象,将我的代码更改为:

@task
def do_consignment_task(agent_id, **store):
    print "GOTHERE IN TASK"
    s = sqlahelper.get_session()
    cObj = USSDConsignmentsObject()
    consign = cObj.gen_consignment()
    ca = Agents.by_id(store.consignment_agents_id)
    consign.consignment_agents_id = ca.id
    consign.consignment_teamleader_id = ca.ou[0].lead_agents_id
    consign.consignment_timestamp = func.now()
    consign.created_by_agent_id = agent_id
    # etc

if self.store.finalise:
    try:
        # del self.service
        store = self.store.__dict__.copy()
        agent_id = self.agent.id
        print store
        print agent_id
        # print help(store)
        do_consignment_task.delay(agent_id, **store)
        transaction.commit()
        #etc

然而,这仍然给出了同样的错误:|

1 个答案:

答案 0 :(得分:2)

尽量不要序列化Pyramid请求对象。当您与芹菜任务进行交互时,您应该将其视为一个独立的过程。

提供完成工作所需的所有信息。请注意,您需要序列化该信息。

因此,self.store可能包含序列化可能不切实际的属性引用。

也许在store对象上创建一个方法,返回一个干净的字典对象。

def serialize(self):
    data = {}
    data["element1"] = self.element1
    data["element2"] = self.element2
    data["element3"] = self.element3
    return data

然后,当您想要调用延迟方法时,请确保使用store.serialize()而不是store或 dict