简体   繁体   English

从django服务器中存在的另一个python脚本引用当前打开的XMPP连接

[英]Referring a currently open XMPP connection from another python script present inside a django server

My application server's requirements are as follows: 我的应用服务器的要求如下:

  1. Receive sensor data from mobile phones (using HTTP) 从手机接收传感器数据(使用HTTP)
  2. Process them (python libraries) 处理它们(python库)
  3. Send notification to the mobile devices (rendered as notifications on Android devices) 向移动设备发送通知(在Android设备上呈现为通知)

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. Django app模块 :为推理库提供HTTP接口,以满足Android设备发送给服务器的HTTP请求。
  • Python Inference Library : Processes the sensor data received from the phones Python推理库 :处理从手机接收的传感器数据
  • GCM App Server Module : explained below GCM App Server模块 :如下所述

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. GCM应用服务器模块 :我使用CCS实现了GCM消息应用服务器 ,该服务器与谷歌的服务器(位于应用服务器和Android设备之间)通信,用于向运行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. GCM云连接服务器(CCS)是一个XMPP端点,可提供与Google服务器的持久,异步,双向连接。 The connection can be used to send and receive messages between your server and your users' GCM-connected devices. 该连接可用于在服务器和用户的GCM连接设备之间发送和接收消息。

In the documentation, they have given a sample python script that I have referred and used to implement my GCM App server. 在文档中,他们给出了一个示例python脚本,我已经引用并用于实现我的GCM App服务器。 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. Python推理库和Django应用程序模块 :我在python中实现了推理库,处理从手机接收的传感器数据。 It has a Django interface to talk to the Android devices. 它有一个Django界面可以与Android设备通信。 The inference library resides inside the Django app server. 推理库驻留在Django应用服务器中。

PROBLEM: 问题:

The GCM App Server script contains a few functions, one of them being send_message(), that sends messages to the Android devices. GCM App Server脚本包含一些函数,其中之一是send_message(),它将消息发送到Android设备。 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. 或者我需要引用持久性开放XMPP连接客户端来发送消息。 I want to avoid putting the processing code in the GCM app server script. 我想避免将处理代码放在GCM应用服务器脚本中。 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. 谢谢。

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. 有很多方法可以解决这个问题,我能想到的一个简单方法是在GCMApp服务器中有一个Queue对象,并在Queue.get()方法上创建一个线程块。 Have the same Queue object shared with the processing django app, and whenever processed data is available push it in the Queue. 将相同的Queue对象与处理django应用程序共享,并且只要处理的数据可用,就将其推送到队列中。 The blocked thread would wake up and send it to the devices. 被阻止的线程将被唤醒并将其发送到设备。 Other way are instead of using the Queue you can use socket. 其他方式是使用socket而不是使用Queue。 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. 另一种方法是使用事件循环, https ://docs.python.org/3/library/asyncio-eventloop.html,这在python 3.0中可用,但您可以查看eventloops。 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. 如果有意义,请告诉我。

Is there a way to do this with my current setup? 有没有办法用我目前的设置做到这一点?

Yes! 是! Using multiprocessing . 使用multiprocessing See answer of this question - Accessing python class variable defined inside the main module of a script 请参阅此问题的答案 - 访问在脚本主模块中定义的python类变量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM