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)
shx2
  • 61,779
  • 13
  • 130
  • 153
High schooler
  • 1,682
  • 3
  • 30
  • 46

1 Answers1

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
shx2
  • 61,779
  • 13
  • 130
  • 153
  • 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. – High schooler Oct 26 '13 at 17:11
  • 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. – shx2 Oct 26 '13 at 17:29
  • Would using a mmap be faster? – High schooler Oct 26 '13 at 18:12
  • It takes ~0.2s to import. My script needs lower response time. – tejasvi88 Sep 05 '21 at 06:13