title: Celery worker introspection to obtain hostname/nodename
body: <p>I want to run py.test integration tests for my Django 1.8 application with
  a full Celery 3.1 setup, that is, with a real queue (not CELERY_ALWAYS_EAGER).</p>
  
  <p>To do that, I need to convince the worker to use my server-based test DB (PostgreSQL) rather than the standard configured DB (SQlite3, which cannot talk to a second process).</p>
  
  <p>To do that, I want to simply modify the <a href="https://docs.djangoproject.com/en/1.8/ref/settings/#databases" rel="nofollow">DATABASES setting</a> in the worker process before the database is first used. (I want to keep SQlite for the other py.test tests as well as the normal non-test DB for interactive testing.)</p>
  
  <p>To do that, the worker needs to recognize that it is running in test mode as opposed to 'normal' mode.
  To indicate test mode, I start the worker with different node names:</p>
  
  <pre><code>celery worker -A myapp --concurrency=1 -n myworker --loglevel=info
  </code></pre>
  
  <p>for normal mode versus</p>
  
  <pre><code>celery worker -A myapp --concurrency=1 -n mytestworker --loglevel=info
  </code></pre>
  
  <p>for test mode.</p>
  
  <p><strong>Question</strong>: How can the celery.py module in my Django application read
  out the nodename (hostname, as set with the <code>-n</code> option) of the worker?</p>
  
  <p>There appear to be three processes involved (I have no further
  configuration for concurrency beyond the above <code>=1</code>):</p>
  
  <ol>
  <li>The <code>celery.exe</code> (I am on Windows), </li>
  <li>the <em>worker</em> itself, and </li>
  <li>the single <em>worker process</em> (which is the relevant one for me).</li>
  </ol>
  
  <p>Is this correct?</p>
  
  <p>There appear to be different <a href="http://celery.readthedocs.org/en/latest/userguide/signals.html" rel="nofollow">signals</a> that celery sends to each process:</p>
  
  <ol>
  <li><code>celeryd_init</code> is sent to <code>celery.exe</code>,</li>
  <li><code>worker_init</code> is sent to the <em>worker</em>,</li>
  <li><code>worker_process_init</code> is sent to the <em>worker process</em></li>
  </ol>
  
  <p>Is this correct, too?</p>
  
  <p>I added the following to <code>celery.py</code> in my Django application:</p>
  
  
  
  <pre class="lang-python prettyprint-override"><code>import celery.signals
  
  def init_here(signal, sender):
      print("call %s(sender=%s)" % (signal, sender))
  
  @celery.signals.celeryd_init.connect
  def celeryd_init(sender, instance, **kwargs):
      init_here("celeryd_init", sender)
  
  @celery.signals.worker_init.connect
  def worker_init(sender, **kwargs):
      init_here("worker_init", sender)
  
  @celery.signals.worker_process_init.connect
  def worker_process_init(sender, **kwargs):
      init_here("worker_process_init", sender)
  </code></pre>
  
  
  
  <p>When I start the worker in test mode as indicated above, I get the following output (among other things):</p>
  
  <pre><code>call celeryd_init(sender=celery@mytestworker)
  call worker_init(sender=celery@mytestworker)
  [2015-09-30 15:53:23,767: WARNING/MainProcess] celery@mytestworker ready.
  [2015-09-30 15:53:24,282: WARNING/Worker-1] call worker_process_init(sender=None)
  </code></pre>
  
  <p>Too bad! The relevant process <code>Worker-1</code> does not receive the relevant information <code>celery@mytestworker</code>.
  How can it obtain the hostname?</p>