Do Signals block Django process?

251 views
Skip to first unread message

Tobias Dacoir

unread,
Jan 30, 2015, 11:13:01 AM1/30/15
to django...@googlegroups.com
I just added django-badges to my project. Basically it can award Badges if Users fullfill certain requirements. For this, everytime I call user.save() django-badges receives the post_save() signal and starts checking.

Now my project is similar to an online quiz, where users submit forms to answer questions. Right now I only have tested it locally and I'm not sure how to simulate multiple users accessing the page at the same time.

When later run on production in combination with a webserver, will Django spawn a new thread for each connection / user? Or will there be only one thread, so if two users are accessing the website, the 2nd has to wait until the request from the first user is processed?

Also what about my signal to process the badges. If I have a function that calls user.save(), which then signals to the badges module, will the execution of the original function stop until the signal has been processed or not?

def myView(request):
   do_something
()
   request
.user.save()
   
# signal emitted
   
# ...
   render
(view)

So in this case, will the view be rendered immediately or only after the receiver for the signal has finished doing it's work? In my case I don't really need it to be asynchronous as long as the badge checking is fast enough and doesn't block the website for other users.

Vijay Khemlani

unread,
Jan 30, 2015, 11:25:23 AM1/30/15
to django...@googlegroups.com
The number of threads is determined by the number of workers in your process that is serving the application, not Django itself.

For example, if you are using uWSGI to serve the application, then you have a parameter "workers" in its initialization file that sets the number of process to spawn and handle incoming requests.

As far as I know, signals are processed "in sync", that is, the original function is stopped until the signal has been processed. If you need async functionality you would need to take a look at celery or something along those lines.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/f3b2f9dd-4681-48a1-8d49-79f73d45d0eb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Tobias Dacoir

unread,
Jan 30, 2015, 2:09:33 PM1/30/15
to django...@googlegroups.com

Thanks for always answering my questions :)

I did read the documentation on uWSGI and I think I have an understanding now of workers and threads (from Webserver or uWSGI) and how it handles multiple users.

For the signals and badge calculation, I did see celery pop up a couple of times and I did briefly look at their documentation. In theory I don't need this though because when the user submits a form, I want to check for achievements before showing him the results page. If I use celery then it might happen that the new view is rendered while the achievements are still being processed. Maybe I will have to go this route at some point but then I face two new problems (one of which I already have):
a) I need to figure out how to get the request that won a badge in order to send him a message (for this I opened an extra thread)
b) I would need to modify his current view, something like using Websocket. I did a quick google search in the past and apparently using websocket and django is not so easy. So if I can avoid it, I'm happy.

If I know that the signals are processed in sync, and before the new view is rendered, then I can just have the code after the signal check if a badge has been awarded and then render the view. Phew, still quite complicated.

Babatunde Akinyanmi

unread,
Jan 30, 2015, 7:57:55 PM1/30/15
to Django users


On 30 Jan 2015 17:13, "Tobias Dacoir" <fal...@gmail.com> wrote:
>
> I just added django-badges to my project. Basically it can award Badges if Users fullfill certain requirements. For this, everytime I call user.save() django-badges receives the post_save() signal and starts checking.
>
> Now my project is similar to an online quiz, where users submit forms to answer questions. Right now I only have tested it locally and I'm not sure how to simulate multiple users accessing the page at the same time.
>
> When later run on production in combination with a webserver, will Django spawn a new thread for each connection / user? Or will there be only one thread, so if two users are accessing the website, the 2nd has to wait until the request from the first user is processed?
>

Django is single threaded but the web server can create multiple instances of your application.

> Also what about my signal to process the badges. If I have a function that calls user.save(), which then signals to the badges module, will the execution of the original function stop until the signal has been processed or not?
>
> def myView(request):
>    do_something()
>    request.user.save()
>    # signal emitted
>    # ...
>    render(view)
>
> So in this case, will the view be rendered immediately or only after the receiver for the signal has finished doing it's work? In my case I don't really need it to be asynchronous as long as the badge checking is fast enough and doesn't block the website for other users.
>

The view will return after the receiver has finished its work. But are you doing any slow queries? I ask because you might be over engineering your work.

A good place to start is to benchmark your queries using a tool like django-debug-toolbar. With that you can know exactly how long it takes one user to query your database and then use that to estimate for more users.

Tobias Dacoir

unread,
Jan 31, 2015, 3:07:15 AM1/31/15
to django...@googlegroups.com
Thanks for the info about django-debug-toolbar. I will certainly test it. I assume that I do a quite a lot of slow queries, but during development I run with a very reduced dataset, so my database (sqlite too instead of mysql) only contains a couple of hundred entries whereas in deployment it will be 10.000+.

Still hopefully I can get some estimates which queries take how long to finish right now and optimize those.

And in a 2nd stage I need some kind of tool to simulate multiple users login in and filling out forms on the site to see how it behaves. I found a couple of names using google, however I still have to settle for one tool that is still maintained and well documented. Any recommendations in that area are welcome.
Reply all
Reply to author
Forward
0 new messages