My post-processing model uses the post_save signal:
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)
this_takes_forever procedure performs an IO, so I want to defer it to avoid too many requests.
I thought this was a great option for the new asyncio module. But itβs not easy for me to understand the whole process.
I think I will need to adapt the signal receiver as follows:
@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()
Provided that this_takes_forever also a coroutine.
@coroutine def this_takes_forever(instance):
Sounds too magical to work. And actually it stops with AssertionError :
AssertionError at /new/ There is no current event loop in thread 'Thread-1'.
I do not see where I should start the cycle in this context. Has anyone tried something like this?
source share