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 :\