Using both Django and Websocket - python

My issue comes as follows: I want to mount a game using only one port (80), and django.
For my game (it's not a MMORTS game, so I must have real-time interaction) I need to use Websockets. This means, I need to use Django (backend, profiles) + Websockets (The game itself). For the sake of this issue, think about this game as if it was like Habbo Hotel or Sims (I need that kind of interaction).
Question: What non-dead packages could I use for this?
If the recommended websockets-package is not integrable to Django but mountable as a separate server, I'd need to solve 2 additional issues:
Is there a way to bridge non-ws connections to the regular Django application handler?
Is there a way to integrate whatever I implement as the websocket handling (i.e. game logic) to the Django engine? (i.e. models).
Assume I'd use this in a shared hosting supporting Python 2.7/Django 1.6.

Related

NodeJS and Python combined architecture?

Could you give me an idea/concepts (not in code) on how could I link NodeJS and Python?
Let's say,
I have NodeJS up and running in PM2 (assuming I already know REST API) and I have a ton of data sets that I need to be ready to display to client side using socket.io (assumming I already know socket.io) as soon as possible.
I'm thinking to use Python. This is for me to implement the basics of machine learning.
In what concept should I start? I'd really love to hear your ideas.
Well you seem to be assuming way too many things, okay from your description I would suggest you to have a look at concept called microservice architecture.
This is how it will work let us assume you want to build an online shopping application where you have 2 main scenarios first is sell all items on your website and second you want to recommend products to your user(Your ML comes into play over here)
So as you said you already know REST API so what you would do is create a microservice (Consider it as a small nodejs application(Using either express or sails or any other framework) which has APIs exposed for all shopping related business logic) also you end up using fromtend technology viz. angularjs for your client side code. You'll show all this shopping stuff by calling your nodejs REST APIs from your angularjs client code. Node provides socket support via socket.io.
Similarly you write a small microservice in python(using Flask and Python-SocketIO) which takes your huge amount of data from datastore does all ML magic and returns recommended products for the particular user(which you received from your angularjs client application), and return it using Python-SocketIO to angularjs(or node application if you're maintaining your frontend logic there instead of angular).
You have provided very less detail so this is abstract view of what you can look into.
Since you're Python oriented for your ML code I'd suggest you to reduce the list of skills you need to learn and/or improve using Python for everything.
You could use Python-SocketIO and Flask, for example.

Accessing the same instance of a class django, python, heroku

I've been working on a website which allows users to play a game against a "Machine" player and I decided to do this using django 1.12 and python 3.6 in an attempt to develop skills in this area. The game & ML algorithms run on the backend in python and during testing/dev this all worked fine. When pushing this to heroku it became apparent that the instance from the game and other classes were being instantiated correctly but then as the page refreshes, in order to get the machine player's choice from the server, the request would go to another server which didn't have the instantiated objects. I tried using the default cache to allow the player to access the same instance but I believe it might be too large. After some reading it sounds like memcached is the way forward, but I wondered whether anyone might have any suggestions or know if there's a simpler solution?
For each request, Django create a new HttpRequest object and load new instances of corresponding views. You can't share data between requests without putting them in a persistent storage.

Deciding to WSGI or Django for new web app

I'm in the process of setting up a new web app and deciding whether to just do it with WSGI or go the full framework route with Django.
The app's foremost requirements:
1) The app has no UI what so ever and all of the data is exposed to clients via a REST api with JSON.
2) It will have data to persist so MongoDB & probably Amazon's SimpleDB will be used for the database side.
Is there a reason to use Django or can I get marginal speed improvement with WSGI only?
Previous server-side apps I've built were either with Java/Struts and Groovy/Grails on the JVM. My understanding is that Django is an MVC framework similar to Rails and Grails.
I've also played around with Google App Engine which uses WSGI as thin layer above your code for managing and routing requests.
I suggest you consider something between those two extremes. Flask is lightweight, very easy to use, and connects to your web server via wsgi. You can use regular python database connectors with it, and a few databases even have Flask-specific extension modules.
I have worked with Django for a couple of projects and I like it a lot, but since you are going to use mongoDB and a lot of JSON I suggest you use NodeJS as server side, with Express as framework, you can see a brief tutorial here:
http://howtonode.org/express-mongodb
One of the advantages of this is that you will use only javascript all along your project, I began working with this technology the last month in a Hackathon, and I can tell you that I'm very impressed of how fast and simple it is.
I've worked a bit with some django "apps" ,its really easy, but setting up the "apps" can be a bit of a long process. Django has a lot of nice features that you won't be using and I agree that you might be on one "extreme" here.

Using Django minus the web server

I'm writing a syndication client, with the aim being to have a client for devices, and a web site that has the same functionality. I shall develop the website using Django - this is already decided; the client shall be written in python with both a CLI and a PyQt4 GUI. I have been writing the clinet first, and it's fairly database-heavy, as everything is cached to enable it to be read while offline.
It struck me today that it would make sense to use Django models for my application, to reduce the repetition of effort between the client and the website. My question is how easy it is to seperate this, and how much of Django I will need in my client to use Django's models. AFAIK I should not need to run the server, but what else is needed? I had an idea of generating the same html for my client as the website, but showing it withing Qt widgets rather than serving pages for a browser.
Has anyone tried this sort of thing before? I'm starting on this already, but it would be good to get a warning of potential dead-ends or things that will create a maintainance nightmare...
Read up on standalone Django scripts and you'll be on your path to victory. Basically all you're really doing is referencing the Django settings.py (which Django expects) and then using models without web views or urls.
If all you're really interested in is using Django's ORM to manage your models and database interaction, you might want to consider using SQLAlchemy instead.
You'll still have to run the Django app as a web server, but you can restrict it to serve to only localhost or something. And sure, you can use QtWebKit as the client.

Django signals as IPC

So I have written a websocket application in Twisted. The application is a basic game between a number of users, but trying to use the web socket for setup and record saving is painful, so I was looking into using Django based rendering for the supplementary information (as in standings, game setup, lobby list, etc) and leave the websockets for the real action. I know I can use some basic IPC functionality to have the Django requests signal the Twisted application, but I was curious if the Django signal system would also work across applications as a simple form of IPC...
No. Django signals are restricted to a single Python interpreter. You'll need to put together something else (sockets, JSON-RPC, XMPP, etc.) in order to perform IPC.

Categories