1

I want my django app to communicate by using a TCP/IP socket with a remote computer and I would like that socket to be available at all times. I would like to use the library tornado. Since I'm only familiar with writing views and models and such, I'm not entirely sure where to fit that into my codebase.

I was thinking about writing a management command that would run the tornado's server (see http://tornado.readthedocs.io/en/latest/tcpserver.html), but how could I call .stop() on my server once the management command quits? I wouldn't want it to spawn any threads that wouldn't exit upon my management command exiting, or end up with multiple open sockets, because I just want one.

Ofcourse I would also like the listener to reside somewhere in my django program and have access to it, not only within the management command code. I was thinking about importing a class from django's settings.

Am I thinking in the right direction, or is there a different, better approach?

EDIT: As to why would I want to do this:

I've got a microcontroller I want to communicate with, and I wouldn't want to go implementing/parsing HTTP on it, and I would also like to periodically send some indication of the connection being alive, and HTTP doesn't seem like the way to go

xtrinch
  • 2,232
  • 1
  • 24
  • 46
  • You don't mention what exactly you're looking to do. Why do you need to *"communicate by using a TCP/IP socket with a remote computer"*? – xyres Nov 21 '17 at 08:37
  • @xyres edited my original question with an explanation – xtrinch Nov 21 '17 at 20:47
  • Okay, but why do you want to stop the server when your management command exists? Don't you want a persistent TCP connection always open? I mean, if all you want to do is have a TCP connection open only while your command is running, then you don't really need Tornado. You can just create a TCP socket using `socket` library in your management command file. So, when your management command exits, the socket will also be closed. – xyres Nov 21 '17 at 21:27
  • @xyres Because I would like to manage my management command with something like systemd, meaning if it's running, I'd like a TCP connection open. – xtrinch Nov 23 '17 at 21:34

1 Answers1

0

Management command is a nice approach, but I'd be reluctant to launch a server using it. A tornado server is a complex thing with a lot of state (including state outside of your codebase, like nginx, apache or HAProxy) and varying health. Management commands aren't designed to deal with all this.

It's probably a nice thing for development, and in this case you can easily make your management command not exit before the server by calling IOLoop.current().start() right inside the command.

For production environment I would advise to use contemporary orchestrating tools like Docker Compose, or if you plan to span your system over several machines, Docker Swarm or Kubernetes. These tools will allow you to start, shut down, scale and check health of individual components in a reliable manner, without reinventing the wheel with a set of management commands.

Either way, if your Tornado code lives in the same place with Django, then you're able to access the database using your Django models and reuse other parts of the project. Besides that, something launched from a management command doesn't get any advantages in using the running Django server.

u354356007
  • 3,205
  • 15
  • 25
  • Meaning I would probably be better off creating pure python code for communicating with my remote machine? How would I communicate with django then? And I didn't understand the part about making my management command not exit before the server? – xtrinch Nov 21 '17 at 20:44
  • @Nirri I'm rather thinking about a single codebase with two executable parts - Django and Tornado. Some sort of orchestration should take care of launching both sides, not a django command - you'll basically have little control over the tornado part then. As for django-tornado communications, as I said, it could be a shared DB; it also could be HTTP API exposed to tornado. It depends on the exact requirements you're setting. – u354356007 Nov 22 '17 at 11:20
  • how does orchestration give me control over tornado? @Vovanrock2002 – xtrinch Nov 22 '17 at 20:40