【问题标题】:Is there any alternative of celery countdown and eta in Faust framework (or other framework)?Faust 框架(或其他框架)中是否有 celery countdown 和 eta 的替代方案?
【发布时间】:2019-10-29 08:06:28
【问题描述】:

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

Celery 中的代码如下:

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

我不希望使用 Celery,并且必须使用 Kafka

【问题讨论】:

  • Faust 是为流(实时数据)处理而设计的,所以 ETA 和倒计时在那里没有真正意义......

标签: python-3.x celery scheduled-tasks countdown faust


【解决方案1】:

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 and timer documentation

【讨论】:

  • crontab 是预定义的。倒计时或 eta 在运行时设置。
【解决方案2】:

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

但它不像内置功能。

@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

【讨论】:

  • 如果您使用等待是为了让其他事情在继续之前发生(这是我能想到的唯一原因),那么您可能需要重组程序以使用一些在进程中阻塞代码,或者让你等待的东西在完成后向 kafka 发布消息。
猜你喜欢
  • 1970-01-01
  • 2010-11-09
  • 2022-01-13
  • 2014-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多