Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tornado WebSocket with Django ORM with shared session

I am creating a python app with chat application. The chat application relies on tornado only. But the chat requires various database access and Django ORM do this beautifully.I am using tornado websockets for chat. So I have following options:

  1. Run both processes on different port and whenever I require to access Django functions I will make a asyncHTTPClient request to other port and get data but resulting in extra load to django server
  2. Run Django inside tornado server and add django project to virtualenv PYTHONPATH and use Django functions directly but this will result in blocking operations

So what should be the best way to merge these two frameworks so they both works good without much change and performance issues.

like image 410
Dheerendra Avatar asked Nov 26 '14 00:11

Dheerendra


1 Answers

It depends on how many times you require to access Django functions from tornado process. If such access count is low, then first approach is better. And if large, then choose second.

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

  • Most of project logic will be in django project. Tornado will just provide means for chatting
  • If you will access the database from tornado, then you'll need to keep in sync your django models and your tornado models. Also, in tornado it is better to use async database driver. So, first approach will avoid this pain.

To my mind, it will be better to implement some REST API at django side and at tornado side, and these processes will communicate via this API with each other. Try to design your architecture in such a way, that you'll need to use this API as rare as possible.

I would recommend to check (or maybe even use) project called centrifuge. It is build on top of tornado and it provides means for implementing real time messages. It has REST api, so you can control it from any other process. In this answer django+centrifuge workflow is described: https://stackoverflow.com/a/26930534/821594

like image 164
stalk Avatar answered Sep 27 '22 20:09

stalk