0

I'm trying to create a decorator that will automatically queue certain functions.

import functools
from rq import Queue
from worker import conn

q = Queue(connection=conn)

def fire_and_forget(func):
    @functools.wraps(func)
    def queue_function(*args, **kwargs):
        try:
            q.enqueue(func, *args, **kwargs)
        except Exception as exc:
            print('QUEUING FAILED')
            print(str(exc))
            print(func)
            return None

    return queue_function

And then I've got some function lets say,

@fire_and_forget
def add(x, y):
    return x + y

However, this does not work because the function that is enqueued is the fire_and_forget function, which enqueues another fire_and_forget function and so on. The workers keep endlessly queueing the task.

What is a way around this? I've been trying to figure it out for hours to no avail :\

2
  • Did you manage to find a solution to this in the end? Commented Mar 7, 2022 at 13:08
  • No I found some other workaround that required me to refactor how I was doing things :( Commented Apr 27, 2022 at 16:24

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.