0

I have a Django app that needs to hit an API call towards an external endpoint on every 5 seconds for a check. Since I do not want to use any external tools like django-background-task, Redis, Celery, RQ or anything I built my own solution where I separated a thread for the task. This is the decorator I found here:

def start_new_thread(function):
    def decorator(*args, **kwargs):
        t = Thread(target = function, args=args, kwargs=kwargs)
        t.daemon = True
        t.start()
    return decorator

And this is how I call the function:

JOB_IN_THE_QUEUE = False

@start_new_thread
def check_if_job_in_the_queue_and_enqueue():
    """Checks if the job is already in the queue and enqueues it if needed."""
    global JOB_IN_THE_QUEUE
    if JOB_IN_THE_QUEUE:
        return
    JOB_IN_THE_QUEUE = True
    success = print_something()
    # print_something() is just a dummy function

And here is how I print indefinitely (that will later become an API call towards a 3rd party API):

def print_something():
a = print_it()
if not a:
    time.sleep(20)
    print_something()
    # print_something() is another dummy function with while True loop replacing the actual API calling

Now the way I am currently calling the check_if_job_in_the_queue_and_enqueue() is at an endpoint. I am waiting for a get request that I am sending once Django is up to trigger the job in the new thread and it works good.

My question is: is there a way for me to trigger this without having to hit the endpoint with a GET (or whatever call, really)?

0 Answers0