Accessing the current open XMPP connection from another python script present on the django server
Requirements for my application server:
- Get sensor data from mobile phones (using HTTP)
- Process them (python libraries)
- Send notification to mobile devices (appears as notifications on Android devices)
Implementation:
To do this, my server has three modules:
- Django app module . Provides an HTTP interface for an inference library for serving HTTP requests sent by Android devices to a server.
- Python output library : processes sensor data received from phones
- GCM Application Server Server : Explained Below
GCM Application Server Server . I have implemented a GCM Message Server using CCS that speaks to google servers (which sits between the app server and android devices) to deliver messages to / from Android mobile devices. Further from their official website (previous link):
GCM Cloud Connection Server (CCS) is an XMPP endpoint that provides a persistent asynchronous bidirectional connection to Google servers. A connection can be used to send and receive messages between your server and GCM devices connected to it.
In the documentation, they provided a sample python script that I mentioned and used to implement my GCM application server. This implementation runs as a standalone script that runs forever.
Python Output Library and Django Application Module . I have implemented an output library in python that processes sensor data received from phones. It has a Django interface to talk to android devices. The output library is located inside the Django app server.
Problem:
The GCM application server script contains several functions, one of which send_message(),
is which sends messages to Android devices. I need to pass this function into my output library scripts when some processed data is available to send to devices. Or I need to refer to the persistent open client of the XMPP connection to send messages. I want to avoid placing the processing code in the GCM script application server. I have been stuck for weeks trying to find a way to do this.
Is there a way to do this with my current setup or do I need to add another layer / module?
Any help or suggestions would be greatly appreciated.
Thank.
source to share
Is there a way to do this with my current setup?
Yes! Using multiprocessing
. See the answer to this question - Accessing a python class variable defined inside the main script module
source to share
I think your idea is valid here. What you want is a clear separation between processing code and communication code. There are many ways to solve this problem, one easy way I can imagine is to have a Queue object in your GCMApp server and make a stream block in the Queue.get () method. Have the same Queue object shared with the django app handler, and whenever processed data is available, push it into the queue. The blocked stream will wake up and send it to devices. Alternatively, instead of using a queue, you can use a socket. Another way is to have an eventloop, https://docs.python.org/3/library/asyncio-eventloop.html, this is available in python 3.0, but you can look at eventloops in general. I suggest you start with something simple and make it work, then start making it look pretty. Let me know if this makes sense.
source to share