简体   繁体   中英

One python process providing information for another python process

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.

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.

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.

In the end I have used RPyC library ( https://rpyc.readthedocs.org/en/latest/ ). Easy to use and powerful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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