1

With this code, is it possible to query the worker state whether it is in state=1 or state=2?

from celery import Celery
import time

#celery -A CeleryTask worker --loglevel=info

app = Celery("CeleryTask", backend="redis://localhost", broker="redis://localhost")


@app.task
def train():
    for i in range(100):
        if i<5:
            state=1

        else:
            state=2
        time.sleep(10)

    return "hallo"

if __name__ == '__main__':
    result = train.delay()
  

1 Answer 1

1

Celery, being awesome as it is, provides you with mechanism to create your own custom states and update them using the update_state() method.

From the (linked) documentation:

@app.task(bind=True)
def upload_files(self, filenames):
    for i, file in enumerate(filenames):
        if not self.request.called_directly:
            self.update_state(state='PROGRESS',
                meta={'current': i, 'total': len(filenames)})

In your case, all you have to do is call update_state() with meta={"state":X} where X is 1 or 2...

Sign up to request clarification or add additional context in comments.

Comments

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.