Is django server based on blocking code - django

I read somewhere that Django is a blocking code. Does that mean when I deploy my code, the server will be able to serve only one request at a time? Do I need to use some other framework like tornado to solve this purpose? Is Django only meant for development and debugging purpose? If it does not solve the purpose of deployment why not use node.js or some other framework.

It means each thread or process can only serve one request at a time. But your server is normally configured to spin up multiple processes.

Related

How to design django app with integrated gRPC server?

I am currently working on a django project where I need a separate backend application to be able to initiate gRPC requests to my django frontend.
Simply put: I want the backend to be able to access and change objects.
Therefore I need my django project to also start my grpc server while starting up.
I did a lot of research and didn't really find any best practice on that.
As far as I would imagine, I could override the startup command of django to also start my grpc server on a seperate thread. But I am afraid that I will end up with a very bad practice solution.
Is there any recommendable way to do this?
I am excited about every hint :)

Live notification/chat in django

I am making a website with django now and I want to implement a live notification feature like the one on facebook or SE.
I did some research and it seems although there's two options: ajax long polling and websockets, the latter is the way to go.
However, as you know the go to plugin for websocket 'socket.io' turns out to be a node.js plugin and the django port only seems to support python 2 and the project seems pretty much dead. I am using python 2.7 as my project interpreter but I want to future proof myself so that if I upgrade to python3 later, I don't find myself not being able to use this functionality.
So my question is this:
Is there a straight forward and future ready way to implement websocket which will be used to send live notifications and chats in django env?
Django itself is build in blocking manner, i.e. with synchronous approach. So, you cannot open persistent websocket with django app, as it will block entire django thread.
If you want to enable notification/chat within django project environment, i would recommend to use centrifuge. It is written in python, but async (non-blocking) framework is used: tornado.
But, you don't need to even know how it works, as it provides simple REST api to communicate with it.
Simplified workflow, check docs for more details:
Start centrifuge at same server, as your django project (or on another but with low latency between them)
Your front-end will open websocket with centrifuge, not with django project.
When you need to send notification, send it to centrifuge from django via REST api, and centrifuge will deliver it to needed clients!
I've already tried it and it works!
Django doesn't provide what you're looking for out of the box. You'll have to use a third party library. One that works across frameworks is Pusher.
I think you must go for Firebase it gives you awesome synchronization and any how you are going to use chat on frontend so its does not have to do anything with django environment so you can update you backend asynchron in callback with firbase. Also firebase with AngularJS provides you really really awesome three way binding.

How to use Tornado work with Django? Is it a good solution?

I've a huge django project and have to use Instagram API and its subscriptions model to work. For the subscriptions, my server has to be very responsive and be ready to work asynchronously to set up a hook so as to receive notifications once the user posts. Or that's what the documentation suggests. Now will it be a good thing to use Tornado there? Just for that small part or can I do it using Django in an effective way? if so, how?
You can use the WSGI container on top of Tornado to host any WSGI application, including Django, however, when you do that the WSGI application is still running as a blocking application and will not magically be running as an asynchronous application. So, when Django is handling a request there is no ability to handle another request at the same time within Django. The solution at that point is not much different to running a single threaded WSGI server and you would need to have multiple Tornado instances to handle concurrent requests.
So all really depends on what you mean by asynchronous. You certainly can't make use of Tornado's direct asynchronous programming API in Django. Thus there isn't really any great benefit from using Tornado with Django via the WSGI interface.
As I understand you are talking about this paragraph in Instagram docs
You should build your system to accept multiple update objects per payload - though often there will be only one included. Also, you should acknowledge the POST within a 2 second timeout--if you need to do more processing of the received information, you can do so in an asynchronous task.
That's another type of "asynchronous" that Tornado provides.
I think Django + Celery will suite better for this.
Your application will work in this way:
You receive JSON-data from Instagram
Create a celery-task, e.g. instagram_process.delay(request.raw_post_data) or instagram_process.delay(request.body) according to your Django version
Response to Instagram with 200 status code
In instagram_process task you do all your procession - parse JSON, store it do database and anything else you need.
If you want to check X-Hub-Signature you can either do it between steps 1 and 2, or pass this header to the task and verify the signature at step 4.
You can use tornado.wsgi to integrate Tornado with other WSGI compliant frameworks. Check out this demo project for details:
https://github.com/bdarnell/django-tornado-demo

Use Django+Redis+Socket.io to build chat room, where to start?

I've heard of these three giant technologies would allow developers to build pub/sub paradigm which result in rapid server push experience.
I've got background on Django, but non of the other two. So just wondering, where can I kick off?
I currently use Gunicorn as django server, uses Nginx as a proxy to serve static files, uses Haproxy as a front-end load balancer. After I adopt new technology stack, can I keep them still?
You will probably encounter issues using Socket.io (which will try to use websocket) with Nginx. Nginx 1.0 does not support proxying of HTTP/1.1. You can use tcp_proxy to work around it. You might be able to find some forks for Nginx 1.1 that have websocket support though.
Check out this and this.
Start here:
http://gevent-socketio.readthedocs.org
and here:
https://github.com/abourget/gevent-socketio
There are some Django examples as to how to get started. Your tech. stack should allow you to run this without much problems.

Adding django-socketio to existing Apache/mod_wsgi/Django site

Can anyone provide or link to a tutorial for adding django-socketio functionality to an existing Django site that uses Apache and mod_wsgi?
Can they work in parallel or does the runserver_socketio command need to handle all requests?
This Question is related but offers little practical information.
Thanks
You should be able to run the regular site behind a public facing server like Apache, with the runserver_socketio part just serving websockets on a separate port. As described in the question you linked to, you'll need to work out if it's possible to proxy websockets through your web server if that's a requirement for you, but as also mentioned the gevent server used by runserver_socketio is more than capable.
When running separate instances like this, the "out of band" functions won't work, as they depend on shared state:
django_socketio.broadcast(message)
django_socketio.broadcast_channel(message, channel)
django_socketio.send(session_id, message)
You'll also need to add the SOCKETIO_PORT to the regular Django project's settings so that it knows which port to use.