I have a remote server like below which already has an initialized class and i have set protocol config as allow public attrs True.
import rpyc
class SharedClass(object):
def __init__(self,string):
print string
def action(self):
print 'i am doing something'
s=SharedClass('hi')
class MyService(rpyc.Service):
def on_connect(self):
pass
def on_disconnect(self):
pass
def exposed_get_shared(self):
return s
if __name__=='__main__:
from rpyc.utils.server import ThreadedServer
t=ThreadedServer(MyService,port=18861,protocol_config={"allow_public_attrs":True})
t.start()
At the client side if i try to connect directly it is working, whereas when i try to make connection inside a function and return the object i am getting an error
**Client**
**Direct connection**
>>>Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32
>>>Type "help", "copyright", "credits" or "license" for more information.
>>>conn=rpyc.connect('localhost',18861)
>>>share=getattr(conn.root,'get_shared')
>>>share
<bound method MyService.exposed_get_shared of <__main__.MyService
object at 0x011BA698>>
>>>share=getattr(conn.root,'get_shared')()
>>>share
<__main__.SharedClass object at 0x00B6ED30>
>>>share.action()
i am doing something
If i try to do it in a function i am getting an error ;(
>>>def returnObject(objName, host, port):
... conn = rpyc.connect(host, port)
... print conn
... attr = getattr(conn.root, 'get_' + objName)()
... print attr
... return attr
>>>share=returnObject('shared','localhost',18861)
<rpyc.core.protocol.Connection 'conn2' object at 0x0108AAD0>
<__main__.SharedClass object at 0x00B6ED30>
>>>share
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\site-packages\rpyc\core\netref.py", line 168,
in __repr__
return syncreq(self, consts.HANDLE_REPR)
File "C:\Python27\lib\site-packages\rpyc\core\netref.py", line 69,
in syncreq
return conn().sync_request(handler, oid, *args)
AttributeError: 'NoneType' object has no attribute 'sync_request'
My purpose is to have an object initialized in server and to have many client access it. The initialized class is thread safe and so multiple clients can use it.
I realize that i am missing something while doing this.
--
Adhithya
Though I have no real experience with rpyc, from your example it seems the only real difference is when executing the returnObject function the 'conn' reference is not retained, so I am guessing the situation has to do with garbage collection of whatever is referenced by 'conn'. With a slight modification to the returnObject function so that 'conn' is returned and retained outside the function, the example seems to execute.
As I am involved in development of another python remote-object solution Versile Python, just for fun here is an alternative implementation using that solution (requires VPy dev release 0.7.2, next version will have some conflicting API changes). Remote service providing SharedClass access:
Client code to access the service: