I know that similar questions occasionally appear
communication between 2 programs in python or Communication between two python scripts
but my problem is much more simple. I have two python processes continuously running on the same computer and slave process occasionally needs three float numbers from master process.
Not being a programming expert, I would reserve three float slots somewhere in computer memory, which would be constantly updated by the master process, and the slave process would simply read this memory slots when it needed information. So essentially master is talking all the time and slave is listening only when it needs information.
Is that possible to do without much fuss and effort? If possible, keep answers simple, as I am not an programming expert.
-
have you considered using sockets to communicate between the two processes?James Kent– James Kent2015年04月09日 11:06:08 +00:00Commented Apr 9, 2015 at 11:06
-
@JamesKent I didn't, can you please provide some good link for starters? (I came across of it but it seemed to be connected to Internet communication and not inter-computer communication.)Pygmalion– Pygmalion2015年04月09日 11:15:48 +00:00Commented Apr 9, 2015 at 11:15
-
tutorialspoint.com/python/python_networking.htm is a reasonable resource for socket programming, as a general rule you would usually use it between machines or over the internet, but it can easily be used for inter process communications, especially if there is a dedicated master process that hosts the port and other processes connect to the port, and depending on the IP/port you chose to use you can do one to one, one to many (with individual data streams) and one to many, (same data to all listeners) via multicast, all depending on what you want to achieve.James Kent– James Kent2015年04月10日 13:48:44 +00:00Commented Apr 10, 2015 at 13:48
3 Answers 3
It depends on the level of programming expert you're not, how quickly you need these numbers to be updated, and what OS you're on.
All that being said, the "Easy" way is simply to write them to a file (or files) that the slave process monitors for change, and reads when they're updated.
4 Comments
Could the two processes (foo and bar below) not simply be threads of a master process?
import time
import threading
x = 0.0
y = 0.0
z = 0.0
lock = threading.RLock()
def foo():
global lock
while True:
time.sleep(10)
with lock:
print x, y, z
def bar():
global x, y, z, lock
while True:
time.sleep(2)
with lock:
x += 0.5
y += 0.6
z += 0.7
fooThread = threading.Thread(target=foo)
fooThread.daemon = True
barThread = threading.Thread(target=bar)
barThread.daemon = True
barThread.start()
fooThread.start()
The 'lock' may not be required, but is good practice in multithreaded programming.
2 Comments
In the end I have used RPyC library (https://rpyc.readthedocs.org/en/latest/). Easy to use and powerful.