Search code examples
pythonflaskpython-huey

Getting task result with python huey from redis data store


enter image description here

I'm working with the huey task queue https://github.com/coleifer/huey in flask . I'm trying to run a task and get a task id number back from my initial function:

@main.route('/renew',methods=['GET', 'POST'])
def renew():
    print(request.form)
    user =request.form.get('user')
    pw =request.form.get('pw')
    res =renewer(user,pw)
    res(blocking=True)  # Block for up to 5 seconds
    print(res)
    return res.id

After running this I plug the outputted id (which is the same as the result in the screenshot)

enter image description here

into :

@main.route('/get_result_by_id',methods=['GET', 'POST'])
def get_result_by_id():
    print(request.form)
    id =request.form.get('id')
    from ..tasking.tasks import my_huey
    res = my_huey.result(id)
    if res==None:
        res = 'no value'
    return res

However I'm getting 'no value'

How can I access the value in the data store?


Solution

  • When you are doing res(blocking=True) in def renew() you are fetching the result from the result store and effectively removing it. When you then try to fetch the result again using the id, it will just return nothing.

    You have 2 options to solve this:

    • Either use res(blocking=True, preserve=True) to preserve the result in the result store so you can still fetch it with your second call.
    • Use a storage that uses expiring results like RedisExpireStorage. When configuring this storage while setting up the huey instance, you can specify for how long your results should be stored. This would give you x amount of time to do the second call based on the task/result id.