Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it okay to spawn a thread from django post save signal?

I have post_save signal on one of my models which calls json.dumps on a large python dictionary. I want to call this json.dumps in a separate thread so it may not slow down the save call on my model. I was wondering if it is okay to spawn a new thread from inside a post_save signal? I have read that post_save signals are themselves thread so would it be okay to spawn another Python thread from it?

EDIT: I cannot use a celery task for some reason.

like image 321
Rafay Avatar asked Oct 20 '22 20:10

Rafay


2 Answers

It's not a direct answer to your question but I found this hint from one of the answers in Is Django post_save signal asynchronous?. My post_save signal handler creates a Celery task.

like image 186
f01 Avatar answered Oct 22 '22 23:10

f01


I don't know where you would have read that signals are executed in threads, because it is not at all true. Django does not do anything with threads, and neither should you: if you want to execute something out of process, use a task queue system like Celery.

like image 43
Daniel Roseman Avatar answered Oct 22 '22 23:10

Daniel Roseman