How to only have one instance of task running in django-q

702 Views Asked by At

I created a schedule to repeat a task every 29 minutes with a timeout of 29 minutes using Django-q but when looking at the queued admin view(Using DjangoORM) it has created almost 40+ tasks and going. While having 5 of the same task running asynchronously. How do I set it up so that it only runs one task at a time one after the other?

This is my current setup:

# settings.py
Q_CLUSTER = {
    'name': 'cams',
    'workers': 4,
    # 'timeout': 90,
    # 'retry': 120,
    'queue_limit': 50,
    'bulk': 10,
    'orm': 'default'
}

# myapp/tasks.py

# temp method to mimic how long my actual task takes to finish executing.
def test():
    sleep(1740) # sleep for 29 minutes

# myapp/schedules.py

# test schedule to run the correct task
def test_schedule():
    schedule(
        func='myapp.tasks.test',
        name='test method',
        schedule_type=Schedule.MINUTES,
        minutes=29,
        repeats=-1,
        q_options={'timeout': 1740, 'retry': 1741},
    )
1

There are 1 best solutions below

0
On

You could set the workers parameter in Q_CLUSTER to 1

workers: 1

This limits the number of worker working on task to be one all the time.