2 years ago

#64197

test-img

Роман Зарьянов

How can I run only a specific function in celery, not the whole files tasks.py?

I have 2 files. The first one is called tasks.py and looks like this

from celery import Celery
import time
app = Celery('tasks', broker='amqp://localhost')
def init():
    for i in range(30):
        print(i)
        time.sleep(1)

init()

@app.task
def add(x, y):
    return x + y

The second one is called run_task.py

from tasks import add
r = add.delay(4, 4)

THe problem is I want the init() function to run only once in a worker (when it is started). Now it runs everytime the run_task.py is executed. How can I do it?

python

celery

0 Answers

Your Answer

Accepted video resources