我尝试将python huey队列合并到我的烧瓶应用程序中,并且我完成了所有工作。我使用它在我的工作流中运行任务,并且在任务运行时,将它从用户隐藏(将huey.task中的task_id添加到数据库中的taskstatus中) - 否则,看起来同样的任务会卡住,但实际上它在后台运行。收听python huey事件 - 运行侦听器一次?
现在棘手的部分是在huey任务完成时显示我的任务。我整合了事件监听器(通过huey.storage迭代),就像huey文档一样,但是从我理解的它所订阅的redis中无限期运行(因为有定期任务检查)。所以,从我了解的事件监听器本身必须在单独的线程中运行,所以我写了休伊任务听休伊任务:
@huey.task(include_task=True)
def get_huey_events(task):
from huey.consumer import EVENT_FINISHED
app = create_huey_app('development')
with app.app_context():
# store huey task id and name in database
task1 = HueyTask(task.task_id, task.name)
db.session.add(task1)
db.session.commit()
for event in huey.storage:
if event['status'] == EVENT_FINISHED:
# consume result to remove from storage
result = huey.result(event['id'])
# remove huey id from my task status - inidicates the task finished - my task will be shown to user
status = WorkflowProcessStatuses.query.filter_by(huey_id=event['id']).first()
if status:
status.huey_id = None
db.session.add(status)
db.session.commit()
但把这样的说法意味着,这样的任务只需要运行一次 - 因为它永远消耗一名工人 - 很快就不会有更多的自由工人。
with app.app_context():
task1 = HueyTask.query.filter_by(name='queuecmd_get_huey_events').first()
pipe = redis.StrictRedis()
if task1:
exists = pipe.hexists('huey.tasks', task1.id)
else:
exists = 0
if task1 is None or not exists:
if task1 and not exists:
#clean old task if not running now
pipe.hdel('huey.tasks', task1.id)
db.session.delete(task1)
db.session.commit()
result = get_huey_events()
pipe.hset('huey.tasks',result.task.task_id,'Event listener')
,所以我得到我的存储get_huey_events的休伊任务ID,看看它是否被存储在Redis的数据库在我的自定义与创建huey.tasks名:在创建应用工厂运行这样我开始上述get_huey_events任务task.id键。如果在数据库中有任务,但不在redis中,或者数据库中没有任何任务,那么我运行事件侦听器,否则不会。
我的问题是 - 有没有更好的方法来做到这一点?事件监听器本身应该是huey.task吗?我现在在windows下运行它。