I have implemented a rpyc sharing server which has an initialized
class object and clients will access this shared object. I am able to
access this object if i connect directly but if i use function for
connecting to server and return the object i am getting error. Below
is the code

**Server**
===========
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): # this is an exposed method
        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
"""
>>>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
"""
**Using a function**
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'

Reply via email to