简体   繁体   English

Python脚本之间的通信

[英]Communication between Python Scripts

I have 2 python scripts. 我有2个python脚本。 1st is Flask server and 2nd Is NRF24L01 receiver/transmitter(On Raspberry Pi3) script. 第一个是Flask服务器,第二个是NRF24L01接收器/发送器(On Raspberry Pi3)脚本。 Both scripts are running at the same time. 这两个脚本同时运行。 I want to pass variables (variables are not constant) between these 2 scripts. 我想在这两个脚本之间传递变量(变量不是常量)。 How I can do that in a simplest way? 我怎么能以最简单的方式做到这一点?

How about a python RPC setup? 如何设置python RPC? Ie Run a server on each script, and each script can also be a client to invoke Remote Procedure Calls on each other. 即在每个脚本上运行服务器,每个脚本也可以是一个客户端,可以相互调用远程过程调用。

https://docs.python.org/2/library/simplexmlrpcserver.html#simplexmlrpcserver-example https://docs.python.org/2/library/simplexmlrpcserver.html#simplexmlrpcserver-example

I'd like to propose a complete solution basing on Sush's proposition. 我想基于Sush的主张提出一个完整的解决方案。 For last few days I've been struggling with the problem of communicating between two processes run separately (in my case - on the same machine). 在过去的几天里,我一直在努力解决两个进程之间的通信问题(在我的情况下 - 在同一台机器上)。 There are lots of solutions (Sockets, RPC, simple RPC or other servers) but all of them had some limitations. 有很多解决方案(套接字,RPC,简单RPC或其他服务器),但它们都有一些限制。 What worked for me was a SimpleXMLRPCServer module. 对我有用的是SimpleXMLRPCServer模块。 Fast, reliable and better than direct socket operations in every aspect. 快速,可靠,优于各方面的直接套接字操作。 Fully functioning server which can be cleanly closed from client is just as short: 可以从客户端干净地关闭的功能齐全的服务器也很短:

from SimpleXMLRPCServer import SimpleXMLRPCServer
quit_please = 0

s = SimpleXMLRPCServer(("localhost", 8000), allow_none=True) #allow_none enables use of methods without return
s.register_introspection_functions() #enables use of s.system.listMethods()
s.register_function(pow) #example of function natively supported by Python, forwarded as server method 

# Register a function under a different name
def example_method(x):
    #whatever needs to be done goes here
    return 'Enterd value is ', x
s.register_function(example_method,'example')

def kill():
    global quit_please
    quit_please = 1
    #return True
s.register_function(kill)

while not quit_please:
    s.handle_request()

My main help was 15 years old article found here . 我的主要帮助是这里发现的15年文章。

Also, a lot of tutorials use s.server_forever() which is a real pain to be cleanly stopped without multithreading. 此外,许多教程使用s.server_forever() ,这是一个真正的痛苦,没有多线程就可以干净地停止。

To communicate with the server all you need to do is basically 2 lines: 要与服务器通信,您只需要做两行:

import xmlrpclib
serv = xmlrpclib.ServerProxy('http://localhost:8000')

Example: 例:

>>> import xmlrpclib
>>> serv = xmlrpclib.ServerProxy('http://localhost:8000')
>>> serv.example('Hello world')
'Enterd value is Hello world'

And that's it! 就是这样! Fully functional, fast and reliable communication. 功能齐全,快速可靠的通信。 I am aware that there are always some improvements to be done but for most cases this approach will work flawlessly. 我知道总会有一些改进,但在大多数情况下,这种方法将完美无缺。

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

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