title: How to correctly shut down Python RQ worker processes dynamically?
body: <p>Using <a href="http://python-rq.org/" rel="nofollow noreferrer">Python RQ</a>, we are trying to dynamically manage worker processes.  We use a custom-built worker script, which (in simplified form) is as follows:</p>
  
  <pre><code>from rq import Connection, Worker
  
  queues_to_listen_on = get_queues_to_listen_on()
  
  with Connection(connection = get_worker_connection()):
      w = Worker(queues_to_listen_on)
      w.work()
  </code></pre>
  
  <p>We are particularly interested in the shutdown of workers.  The main concern we have is how to do a graceful shutdown of a worker, in a way that will enable current work to be finished before shutting down.  The <code>request_stop(...)</code> signal handler on the appropriate <code>Worker</code> object seems to do what we need, but there seems to be no way (at least to my knowledge) of emitting it unless it is through pressing <code>CTRL+C</code> on the worker process running in the terminal.</p>
  
  <p>As I see it, there are two probable solutions (there could definitely be more) - in order of preference:</p>
  
  <ol>
  <li>Programmatically, using the <code>rq</code> library, send the signal to <code>request_stop</code> and thus trigger graceful shutdown.</li>
  <li>Somehow acquire the pid of the correct process (not sure if the workhorse process or the worker listener process) and using some other method, send the appropriate signal to that process.  We have some ways this could be done, but it would very probably require more work and introduce other variables to the problem that I would prefer be left out (for example, using <code>Fabric</code> to run a remote command or something along those lines).</li>
  </ol>
  
  <p>If there is a better way to solve this problem or a different alternative that would achieve the same goal, I would appreciate your suggestions.</p>