title: Run pika ioloop in background or use custom ioloop
body: <p>I have a feeling that this should really not be all that difficult, yet I have had little success so far.</p>
  
  <p>Say I have a class called PikaClass that wraps <a href="http://tonyg.github.com/pika/" rel="noreferrer">pika</a> and provides some business methods.</p>
  
  <pre><code>def PikaClass(object):
    def __init__(self):
       # connect to the broker
       self.connection = pika.SelectConnection(&lt;connection parameters&gt;, self.on_connect)
       # ..other init stuff..
  
    def on_connect(self, connection):
       # called when the connection has been established 
       # ..open a channel, declare some queues, etc.
  
    def start(self):
       # start the polling loop 
       self.connection.ioloop.start()
  
    def foo(self, **kwargs):
       # do some business logic, e.g., send messages to particular queues
  </code></pre>
  
  <p>Intuitively, this is what I would like to achieve: a user creates an instance of <code>PikaClass</code>, sets the loop going in the background, and then interacts with the object by calling certain business methods</p>
  
  <pre><code>p = PikaClass()
  p.start()
  bar = p.foo(..)
  </code></pre>
  
  <p>The problem is that p.start() blocks and prevents the main code from interacting with the object once start() has been called.  My first thought was to wrap the call in a thread:</p>
  
  <pre><code>Thread(target=p.start()).start()
  bar = p.foo(..)
  </code></pre>
  
  <p>But that still blocks and you never get to p.foo(..).  The docs mention that you shouldn't share a connection between threads so that may cause a problem somewhere.</p>
  
  <p>I have also tried using AsyncoreConnection instead of SelectConnection, and calling _connect() directly (instead of using the ioloop) but that does not have any effect (nothing happens).</p>
  
  <p>So how can I run the ioloop in the background, or at least run my own ioloop?</p>
  
  <p>Note: This is Python 2.6 on win64 (xp) with the latest pika 0.9.4</p>