繁体   English   中英

是否可以将模型对象连接到芹菜任务?

[英]Is it possible to connect a model object to a celery task?

我需要将芹菜任务连接到模型对象。 例如,我需要创建一个模型对象

class AuthorPrice(models.Model):
    author = models.Charfield(default=0)
    price = models.FloatField(default=0)

我有一个task.py

app = Celery()

@app.task
def create():
    new = AuthorPrice.object.create()
    new.author = John
    new.price = 30
    new.save()

我称任务为视野

create.apply_async(eta.datetime(2019, 07, 31, 15, 56))

到目前为止,一切都还可以,但是,如果我需要撤消或编辑此任务,可以像外键一样将其连接到我的模型上吗?

ty

编辑1:

假设我在今天下午15:30排队发送任务,并告诉它创建模型对象。

之后,我需要在该模型对象中编辑某些内容,任务时间不再是15:30,而是16:30 ...

现在我的模型是:

class AuthorPrice(models.Model):
    author = models.Charfield(default=0)
    price = models.FloatField(default=0)
    task = models.Charfield(default=0)

我的任务是:

@app.task(bind=True)
def create(self):

    print app.AsyncResult.task_id
    new = AuthorPrice.objects.create()
    new.author = 'John'
    new.task = app.AsyncResult.task_id
    new.save()

它在db中写了一个task_id somethign

<property object at 0x7fc77a397b50>

但是如果我需要撤消它是行不通的...

我的目标是在某处备份task_id,并在我更改任务本身的某些内容时将其撤消。

有任何想法吗?

您可以将任务的ID设置为任意值,并在处理任务之前使用此ID撤销任务

import uuid
task_id = uuid.uuid4()
create.apply_async(task_id=task_id)
some_data_storage.set(key, task_id)

当您想撤销时

task_id = some_date_storage.get(key)
AsyncResult(task_id).revoke()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM