Tornado WebSocket with Django ORM with shared session

I am creating a python application with a chat application. The chat application relies only on tornadoes. But chat requires different access to the database, and Django ORM does it beautifully. I use tornado web chats for chat. Therefore, I have the following options:

  • Run both processes on a different port and whenever I need access to Django functions, I will make an asyncHTTPClient request to another port and get the data, but as a result you will get additional load on the django server.
  • Run Django inside the tornado server and add the django project to virtualenv PYTHONPATH and use the Django functions directly, but this will lead to blocking operations

So, what should be the best way to combine these two structures so that they both work well without much change or performance issues.

+5
source share
1 answer

It depends on how many times you need access to Django features from the tornado process. If such an access indicator is small, then the first approach is better. And if large, then choose the second.

But I would try to implement the first approach, because:

  • Most of the project logic will be in the django project. A tornado will simply provide a means of communication
  • If you access the database from a tornado, you will need to synchronize your django models and your tornado models. In addition, in a tornado, it is better to use the async database driver. Thus, the first approach will avoid this pain.

In my opinion, it would be better to implement some REST API on the django side and on the tornado side, and these processes will interact through this API with each other. Try to design your architecture in such a way that you will need to use this API as little as possible.

I would recommend checking out (or maybe even using) a project called centrifuge . It is built on top of a tornado and provides facilities for real-time messaging. It has a REST api, so you can control it from any other process. This answer describes the django + centrifuge workflow : fooobar.com/questions/616866 / ...

+3
source

Source: https://habr.com/ru/post/1207704/


All Articles