title: How can I set up Celery to call a custom worker initialization?
body: <p>I am quite new to Celery and I have been trying to setup a project with 2 separate queues (one to calculate and the other to execute). So far, so good. </p>
  
  <p>My problem is that the workers in the execute queue need to instantiate a class with a unique object_id (one id per worker). I was wondering if I could write a custom worker initialization to initialize the object at start and keep it in memory until the worker is killed.</p>
  
  <p>I found a similar question on <a href="https://stackoverflow.com/questions/2129820/how-can-i-set-up-celery-to-call-a-custom-initialization-function-before-running">custom_task</a> but the proposed solution does not work in my case.</p>
  
  <p>Considering the following toy example:</p>
  
  <p>celery.py</p>
  
  <pre><code>from celery import Celery
  
  app = Celery('proj',
               broker='amqp://guest@localhost//',
               backend='amqp://',
               include=['proj.tasks'])
  
  app.conf.update(
      CELERY_TASK_RESULT_EXPIRES=60,
      CELERY_ROUTES = {"proj.tasks.add1": {"queue": "q1"}},
  )
  
  if __name__ == '__main__':
      app.start()
  </code></pre>
  
  <p>tasks.py</p>
  
  <pre><code>from proj.celery import app
  from celery.signals import worker_init
  
  @worker_init.connect(sender='worker1@hostname')
  def configure_worker1(*args, **kwargs):
      #SETUP id=1 for add1 here???
  
  @worker_init.connect(sender='worker2@hostname')
  def configure_worker2(*args, **kwargs):
      #SETUP id=2 for add1 here???
  
  @app.task
  def add1(y):
      return id + y
  
  @app.task
  def add(x, y):
      return x + y
  </code></pre>
  
  <p>initializing:</p>
  
  <pre><code>celery multi start worker1 -A proj -l info -Q q1
  celery multi start worker2 -A proj -l info -Q q1
  celery multi start worker3 -A proj -l info
  </code></pre>
  
  <p>Is this the right approach? If so, what should I write in the <code>configure_worker1</code> function in <code>tasks.py</code> to setup <code>id</code> at the worker initialization?</p>
  
  <p>Thanks </p>