1

I have 2 python scripts. 1st is Flask server and 2nd Is NRF24L01 receiver/transmitter(On Raspberry Pi3) script. 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?

2 Answers 2

2

How about a python RPC setup? I.e. 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

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. I will try. If I would use C, C++ I could use pointer to do that? But python doesn't support pointers, right? Another my Idea is to merge both scripts, but I am afraid then I will have slow program?
Why do you think you'll have a show program, what level of responsiveness do you need?
1

I'd like to propose a complete solution basing on Sush's proposition. 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. What worked for me was a SimpleXMLRPCServer module. 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.

Also, a lot of tutorials use s.server_forever() which is a real pain to be cleanly stopped without multithreading.

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.