remote class and object from client

24 views
Skip to first unread message

Simon Burke

unread,
Apr 12, 2015, 2:10:08 PM4/12/15
to rp...@googlegroups.com
Folks
  have been playing around a little with RPyC and have a few dumb questions that have come up that i hope someone more enlightened can help answer.

My "goal" is more of a "thought experiment" than production code , so i can understand  RPyC a little better.

Assuming i have this little program

#!/usr/bin/python3

import os

# this is a simple  class to
# get the host pid
class MyClass(object):
 
def getPID(self):
   
return os.getpid()
 
myobject
= MyClass()
print ("OBJECT running as pid " + str(myobject.getPID()) )

then i'd like to be able to "remote" both the class and object to a remote RPyC server with minimal changes to code i:e

start an RPyC server in "classic" mode

/usr/local/bin/rpyc_classic.py

and change code to

#!/usr/bin/python3


import os
import rpcobject

# this is a simple  class to
# get the host pid
class MyClass(rpcobject):
 
def getPID(self):
   
return os.getpid()
 
myobject
= MyClass()
print ("OBJECT running as pid " + str(myobject.getPID()) )

which should "remote" both the class defn and the object instance to the RPyC server and give me the RPyC server PID instead of the local program PID

the goal is to minimize the changes to an exisiting program and to be able to simply "remote" all instances of a class to a  remote server (or process)

the more advanced version of the above should be able to handle this so objects can be moved to different RPyC servers on same or different host/ports

#!/usr/bin/python3


import os
import rpcobject


# this is a simple  class to
# get the host pid
class MyClass(rpcobject):
 
def getPID(self):
   
return os.getpid()
 
myobject
= MyClass(rpcHost="localhost", rpcPort="18812")
print ("OBJECT running as pid " + str(myobject.getPID()) )

and for record, yes i understand the security implications of the above, this is NOT production code its a thought experiment :-)

So implemented a version of rpcobject that does the above and it seems to work for me just fine. But out of that process a couple of questions case up

1.. First and fore most, my implementation (below) seems like i am re-inventing a wheel and i think i am missing something. Is there a more elegant "RPyC" way to solve this problem ?

2.. my implementation creates a new connection for each instance, is that ok or should i try to share connections to same host/port ?

3.. there's a problem when i try to create an __init__ function in MyClass and it has to look like this

 def __init__(self,id=None,**kwargs):
   
print ("MyTestClass2::__init__(" + str(id) + ")")


I have to add the **kwargs to the __init__  is to consume the rpcHost etc options in the "advanced" example. I tried creating a metaclass (pyhton 3) to intercept the __call_ to remove them from the kwargs list prior to __init__ but RPyc doesn't seem to like me using metaclasses. Is there a clean way to "solve" this and still have the class be "remoteable" ?


Full test case is attached for reference

Any enlightenment would be greatly appreciated











client4.py
Reply all
Reply to author
Forward
0 new messages