Search code examples
pythonmysqldjangosocketstornado

How to insert data from a tornado web-socket to a mySQL database using Django


I am using Django with mysql to create a monitoring web page. I have also created a web-socket server with tornado. What I want to do is to insert data in mysql database every time I receive something from the web-socket.

What kind of connections I should do between Tornado and the database? It's better to send the data to Django and then make the inserts into the database or create a python program for doing that?

I am quite lost in how exchange data between django and tornado.

After the answer of EvilX:

I have create a function to insert the data in the database in tornado server file but I receive an error:

django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

The tornado server file Is like this:

import tornado.httpserver
import tornado.websocket    
import tornado.ioloop    
import tornado.web
import json
from storage.models import System, Flywheel, Sensor, SensorData
from threading import Thread
import os


def dataInsert():  

string='{"jSonString":value}'

jdata=json.loads(string)

for system_name in jdata.keys():
    system = System.objects.get(name= system_name)
    for flywheel_name in jdata[system_name].keys():
        flywheel=Flywheel.objects.get(system=system.id,name=flywheel_name)

        for sensor_name in jdata[system_name][flywheel_name].keys():

            sensor= Sensor.objects.get(flywheel=flywheel.id,sensorType=sensor_name)

            sd=SensorData(sensor=sensor, value= jdata[system_name][flywheel_name][sensor_name])
            sd.save()





class WSHandler(tornado.websocket.WebSocketHandler):
def check_origin(self, origin):
    return True
def open(self):
    print 'new connection'
    self.write_message("Hello World")


def on_message(self, message):
    #self.write_message(u"Your message was: " + message)
    #print 'message received %s' % message

def on_close(self):
  print 'connection closed'

application = tornado.web.Application([
(r'/ws', WSHandler),])



if __name__ == "__main__":  
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings' # TODO: edit this
#sys.path.append('./demosite') # path to your project if needed

thread = Thread(target=dataInsert())
thread.start()
thread.join()

http_server = tornado.httpserver.HTTPServer(application)  

http_server.listen(6666)

tornado.ioloop.IOLoop.instance().start()

Solution

  • There are two ways.

    1. With rabbitmq. Create workers with django (celery), and send tasks to queue from tornado.

    2. Add to tornado run script: os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'

    Then simple use django orm for work with database. Use it with threads, because django orm is not async. Also dont forget restart tornado one time at day if you receive issue "Mysql has gone away".