Are django signals always synchronous?

2019-06-05 12:30发布

问题:

I'm developing a django IPN plugin that saves IPN data to a model and then calls a post_save signal.

I'm worried that under this use case (gunicorn, gevent, etc) that the signals may be called/completed asynchronously. The IPN often sends more than 1 request to the ipn url, and I need to be able to process those requests in order.

Should I use a queue for this? Would simple python queue's work better, or should I use something like kombu + celery (with 1 worker)?

回答1:

Not sure synchronicity is your real concern here. Django's signals are always executed in-process: that is, they will be executed by the process that did the rest of that request, before returning a response. However, if your server itself is asynchronous, there's a possibility that one request will finish processing after a second one that was received later, and therefore the signals will be processed in the wrong order.

Celery, of course, is definitely asynchronous, but might well be a better bet if reliable ordering is a priority for you.