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)
7
  • Do you mean: python main.py & python otherscript.py ? Commented Oct 26, 2013 at 16:06
  • 1
    He probably wants threading. Commented Oct 26, 2013 at 16:14
  • Wow no, I am running main.py at startup. Then otherscript.py at some other time (whenever the user wants) Commented Oct 26, 2013 at 16:22
  • @KingofGames so you want to access other python process's variable? Commented Oct 26, 2013 at 16:23
  • 1
    I want to access another process's object I suppose Commented Oct 26, 2013 at 16:24

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

4 Comments

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.
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.
Would using a mmap be faster?
It takes ~0.2s to import. My script needs lower response time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.