[英]How can i run multiple tasks with parameters on huey?
I made the settings similar to those of celery我做了类似于 celery 的设置
SCHEDULE_SETTINGS = {
'x': {
'task': 'app.tasks.x',
'schedule': crontab(minute='*/1'),
'kwargs': dict(xx='some')
},
'y': {
'task': 'app.tasks.y',
'schedule': crontab(minute='*/1'),
},
}
Functions功能
def x(xx):
print('func x', xx)
def y():
print('func y')
launching tasks启动任务
from importlib import import_module
from app.tasks.config import huey
def tasks_bootloader():
for name, settings in SCHEDULE_SETTINGS.items():
task = settings['task']
schedule = settings['schedule']
kwargs = settings.get('kwargs')
full_path = task.split('.')
func_name = full_path[-1]
module_path = '.'.join(full_path[:-1])
module = import_module(module_path)
func = getattr(module, func_name)
def wrapper():
if kwargs:
func(**kwargs)
else:
func()
huey.periodic_task(schedule, name=name)(wrapper)
Result:结果:
[2020-12-03 23:42:07,572] INFO:huey.consumer.Scheduler:Scheduler:Enqueueing periodic task app.x: ae0628de-3df7-429b-a87f-35894db214ce.
INFO:huey.consumer.Scheduler:Enqueueing periodic task app.x: ae0628de-3df7-429b-a87f-35894db214ce.
[2020-12-03 23:42:07,574] INFO:huey.consumer.Scheduler:Scheduler:Enqueueing periodic task app.y: 549b6338-b352-494b-93ac-c106af05177d.
INFO:huey.consumer.Scheduler:Enqueueing periodic task app.y: 549b6338-b352-494b-93ac-c106af05177d.
[2020-12-03 23:42:07,582] INFO:huey:Worker-1:Executing app.x: ae0628de-3df7-429b-a87f-35894db214ce
INFO:huey:Executing app.x: ae0628de-3df7-429b-a87f-35894db214ce
func y
[2020-12-03 23:42:07,585] INFO:huey:Worker-1:app.x: ae0628de-3df7-429b-a87f-35894db214ce executed in 0.003s
INFO:huey:app.x: ae0628de-3df7-429b-a87f-35894db214ce executed in 0.003s
[2020-12-03 23:42:07,586] INFO:huey:Worker-1:Executing app.y: 549b6338-b352-494b-93ac-c106af05177d
INFO:huey:Executing app.y: 549b6338-b352-494b-93ac-c106af05177d
func y
Everything works, but only work the last task.一切正常,但只完成最后一项任务。
I need this way of launching.我需要这种启动方式。 In production, in the wrapper, tasks will be launched in threads.在生产中,在包装器中,任务将在线程中启动。
Can someone have any ideas?有人可以有任何想法吗?
I did it.我做的。
We need a new instance of the function every time - the __call__ method我们每次都需要一个新的 function 实例 - __call__ 方法
Implemented multi-launch of tasks on huey with parameters passing.通过参数传递在huey上实现了多次启动任务。 With settings similar to celery.设置类似于 celery。
class LaunchingWrapper(object):
def __init__(self, func, kwargs):
self.func = func
self.kwargs = kwargs
def __call__(self):
if self.kwargs:
self.func(**self.kwargs)
else:
self.func()
def tasks_bootloader():
for name, settings in SCHEDULE_SETTINGS.items():
task = settings['task']
schedule = settings['schedule']
kwargs = settings.get('kwargs')
full_path = task.split('.')
func_name = full_path[-1]
module_path = '.'.join(full_path[:-1])
module = import_module(module_path)
func = getattr(module, func_name)
lw = LaunchingWrapper(func, kwargs)
huey.periodic_task(schedule, name=name)(lw)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.