Referring a currently open XMPP connection from an

2019-07-07 00:32发布

问题:

My application server's requirements are as follows:

  1. Receive sensor data from mobile phones (using HTTP)
  2. Process them (python libraries)
  3. Send notification to the mobile devices (rendered as notifications on Android devices)

Implementation Setup:

In order to do the above, my server has three modules:

  • Django app module: Provides an HTTP interface to the inference library to cater to the HTTP requests sent by the Android devices to the server.
  • Python Inference Library: Processes the sensor data received from the phones
  • GCM App Server Module: explained below

GCM App Server Module: I have implemented GCM Message App Server using CCS that talks to the Google's servers (that sits between the app server and Android devices) for delivering messages to/from mobile devices running Android. Following is from their official website (previous link):

The GCM Cloud Connection Server (CCS) is an XMPP endpoint that provides a persistent, asynchronous, bidirectional connection to Google servers. The connection can be used to send and receive messages between your server and your users' GCM-connected devices.

In the documentation, they have given a sample python script that I have referred and used to implement my GCM App server. This implementation is executed as a standalone script that runs forever.

Python Inference Library and Django app module: I have implemented the inference library in python that processes sensor data received from the phones. It has a Django interface to talk to the Android devices. The inference library resides inside the Django app server.

PROBLEM:

The GCM App Server script contains a few functions, one of them being send_message(), that sends messages to the Android devices. I need to refer this function in my inference library scripts when some processed data is available to be sent to the devices. Or I need to refer to the persistent open XMPP connection client to send messages. I want to avoid putting the processing code in the GCM app server script. I have been stuck for weeks to find a way to do the above.

Is there a way to do this with my current setup or do I need to add some other layer/module?

Any help or suggestions would be greatly appreciated.

Thanks.

回答1:

I think your idea here is valid. That you want clear separation between processing code and communication code. There are many ways to solve this problem, one simple way I can think of is to have a Queue object in your GCMApp Server and make a thread block on the Queue.get() method. Have the same Queue object shared with the processing django app, and whenever processed data is available push it in the Queue. The blocked thread would wake up and send it to the devices. Other way are instead of using the Queue you can use socket. Another way is having a 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 would suggest you to start from something simple and get it working and then start making it beautiful. Let me know if it makes sense.



回答2:

Is there a way to do this with my current setup?

Yes! Using multiprocessing. See answer of this question - Accessing python class variable defined inside the main module of a script