简体   繁体   中英

Get the name of celery worker from inside a celery task?

I'd like a celery task to be able to get the name of the worker executing it, for logging purposes. I need to handle this from within the task, rather than querying the broker directly. Is there a way to do this? I'm using celery with RabbitMQ, if that matters.

You need to utilize billiard which holds the workers:

from celery import task
from billiard import current_process

@task
def getName():
    p = current_process()
    return p.index

Then make a global dictionary that maps ids->names on process creation.

I also needed the worker name for reporting purposes so I tried @cacois solution but it doesn't seem to work with eventlet (current_process() doesn't have an initargs attribute). So I'll leave my solution here for future references:

from celery import task

@task(bind=True)
def getName(self):
    return self.request.hostname

The name of the attribute sounds weird to me, but that contains the name specified with the "-n" option when launching the worker. self works like you would expect from a class method, you don't need to specify it when calling the function (eg: getName.delay()).

You were originally looking for the name you put in with the -n flag, right? It's in the initargs array. Here's a modified version of the answer that gets you that:

from celery import task
from billiard import current_process

@task
def getName():
    p = current_process()
    return p.initargs[1].split('@')[1]

Use the celeryd_after_setup signal to capture the worker name like this:

from celery.signals import celeryd_after_setup

@celeryd_after_setup.connect
def capture_worker_name(sender, instance, **kwargs):
    os.environ["WORKER_NAME"] = '{0}'.format(sender)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM