在Huey中为periodic_tasks实现context_task的最佳方法是什么?

问题描述 投票:0回答:1

我正在寻找实施Peewee上下文管理器的最佳方法,在Huey中定期执行任务。正常的任务有很好的小Huey.context_task()装饰器,但似乎没有任何类似的周期性任务。

我是否正确地假设我只需要在周期性任务中使用(丑陋的)声明?

peewee python-huey
1个回答
0
投票

应该可以做这样的事情:

from functools import wraps

huey = RedisHuey()
db = PostgresqlDatabase(...)

def db_periodic_task(*args, **kwargs):
    def decorator(fn):
        @wraps(fn)
        def new_task():
            with db:
                fn()
        return huey.periodic_task(*args, **kwargs)(new_task)
    return decorator

@db_periodic_task(crontab('0', '0'))
def my_periodic_task():
    # db will have been opened at start.
    # db will then be closed upon return.
© www.soinside.com 2019 - 2024. All rights reserved.