How to interact with Running Python Script
Asked Answered
N

1

5

While my main script is running

class Blah():
    update=0

    def testthings(function):
        return function(9)

main = Blah()
while True:
    main.update+=1

How can I run an independent script to have access to the 'main variable' (defined in my main script) while the main script is running?

Something like this would be useful

main = GetMain()
while True:
    main.testthings(lambda x: x)
Nb answered 26/10, 2013 at 16:1 Comment(7)
Do you mean: python main.py & python otherscript.py ?Ashlan
He probably wants threading.Popper
Wow no, I am running main.py at startup. Then otherscript.py at some other time (whenever the user wants)Nb
@KingofGames so you want to access other python process's variable?Ashlan
I want to access another process's object I supposeNb
You could have a look at semaphores... That's the only way I know to share process memory. Otherwise, other kind of stuff like sockets maybe...Original
I think he's looking for RPC-based solutionFinder
F
11

Use rpyc. It's clean, intuitive, and very powerful.

Basically, you write a service class which exposes whichever interface you'd like (e.g. it has a method which returns that global variable you need), create a server object associated with that service, and start it. Then, in a client, you connect using a client object, and call that function, which returns the variable from the server process.

EDIT: code sample for running the rpyc server, and connecting an rpyc client

rpyc_main.py

# main definitions
import time
class Blah():
    update=0
    def testthings(self, function):
        return function(9)

# rpyc servic definition
import rpyc

class MyService(rpyc.Service):
    def exposed_testthings(self, function = lambda x: x):
        return main.testthings(function = function)
    def exposed_get_main_update(self):
        return main.update

# start the rpyc server
from rpyc.utils.server import ThreadedServer
from threading import Thread
server = ThreadedServer(MyService, port = 12345)
t = Thread(target = server.start)
t.daemon = True
t.start()

# the main logic
main = Blah()
while True:
    main.update+=1
    time.sleep(1)

rpyc_client.py

# rpyc client
import rpyc
conn = rpyc.connect("localhost", 12345)
c = conn.root

# do stuff over rpyc
import time
print 'update =', c.get_main_update()
time.sleep(2)
print 'update =', c.get_main_update()
print 'testing returned:', c.testthings(lambda x: x)  # calling a method of the remote service
print 'update =', c.get_main_update()

output

update= 6
update= 8
testing returned: 9
update= 8

Notes:

  1. a lambda object (actually, a rpyc-reference to that object) is passed from the client to the server. when it is being called, it actually runs in the client process. This is super cool and far from trivial. It works because rpyc is symmetric
  2. for ultra-flexibility, use rpyc's classic mode
Finder answered 26/10, 2013 at 16:50 Comment(4)
This looks very interesting, I will have a look. Isn't this a bit of an overkill though? Both of the scripts are ran on the same computer.Nb
rpyc requires the same amount of work whether on same or different computers. the upside is that it's a RPC solution for which you don't need to worry about anything but your python logic. I.e. no sockets, no XMLs (as in SOAP), no fancy protocols, no serialization, etc. It's actually simple. I'll add code to my answer when I find the time.Finder
Would using a mmap be faster?Nb
It takes ~0.2s to import. My script needs lower response time.Mortenson

© 2022 - 2024 — McMap. All rights reserved.