0

我想通过用户设置在特定时间触发一些任务。例如,如果用户设置为下午 4:00,那么我将在下午 4:00 运行任务。它可以在 Celery 中使用倒计时和 eta 进行处理。但我的经纪人更喜欢卡夫卡。有没有 Celery 倒计时和 eta 的替代品?

Celery 中的代码如下:

result = add.apply_async((2, 2), countdown=3)

我希望不要使用 Celery,而必须使用 Kafka

4

2 回答 2

2

如果我正确理解了您对@meherrr 的回复,那么这可能就是要走的路。使用 asyncio.sleep() 并在消息中传递延迟时间可以实现与此处所述相同的行为:https ://docs.celeryproject.org/en/latest/userguide/calling.html#eta-and-倒数

但这不像是内置功能。

@app.agent(some_topic, concurrency=100)
async def do_something_later(things_to_do):
    async for thing in things_to_do:
        delay_by = thing.time_to_wait
        await asyncio.sleep(delay_by)
        result = do_the_thing_to_the(thing)
        yield result
于 2019-11-20T19:53:20.687 回答
2

Faust 确实有一个用于启动进程的 crontab 触发器。下面的示例显示了一个使用 crontab 的简单实现,它将每分钟运行一次作业:

import faust
import asyncio

app = faust.App(
        'some_print_step',
        broker="kafka://localhost:9092",
    )

@app.crontab("* * * * *")
async def run_every_min():
    print("This process will be triggered every minute.")


@app.crontab('0 18 * * *')
async def run_everyday_at_6pm:
    print('This process will be triggered every day at 6pm.')

您可以在此处找到更多 crontab 文档:

Faust crontab 和计时器文档

于 2019-11-18T16:37:23.237 回答