title: Asynchronous signals with asyncio
body: <p>My model post processing is using the <code>post_save</code> signal:</p>
  
  <pre><code>from django.core.signals import request_finished
  from django.dispatch import receiver
  from models import MyModel
  from pipeline import this_takes_forever
  
  
  @receiver(post_save, sender=MyModel)
  def my_callback(sender, **kwargs):
      this_takes_forever(sender)
  </code></pre>
  
  <p>The <code>this_takes_forever</code> routine does IO so I want to defer it to avoid blocking the request too much.</p>
  
  <p>I thought this was a great use case for the new <a href="https://docs.python.org/3/library/asyncio.html" rel="noreferrer">asyncio</a> module. But I have a hard time getting my mind around the whole process.</p>
  
  <p>I think I should be able to adapt the signal receiver like this:</p>
  
  <pre><code>@receiver(post_save, sender=MyModel)
  def my_callback(sender, **kwargs):
      loop = asyncio.get_event_loop()
      loop.run_until_complete(this_takes_forever(sender))
      loop.close()
  </code></pre>
  
  <p>Provided <code>this_takes_forever</code> is also adapted to be a coroutine.</p>
  
  <pre><code>@coroutine
  def this_takes_forever(instance):
      # do something with instance
      return instance
  </code></pre>
  
  <p>This sounds too magical to work. And in fact it halts with an <code>AssertionError</code>:</p>
  
  <pre><code>AssertionError at /new/
  There is no current event loop in thread 'Thread-1'.
  </code></pre>
  
  <p>I don't see where should I start the loop in this context. Anyone tried something like this?</p>