def test_rpc_base_1_to_N(): global port N = 5 # Create 0MQ transport context = zmq.Context() cli = context.socket( zmq.REQ ) cli.bind( "tcp://*:%d" % ( port )) # Create the test server and connect client to it thr = [] for i in xrange( N ): svr = context.socket( zmq.XREP ) svr.connect( "tcp://localhost:%d" % ( port )) svrthr = zmqjsonrpc.server_thread( root=globals(), socket=svr ) svrthr.name = "Server-%d" % ( i ) svrthr.start() thr.append( svrthr ) port += 1 # Create callable to method "first" and invoke; then "second" remboo = zmqjsonrpc.client( socket=cli, name="boo" ) result = remboo.first( "some", "args" ) assert result.endswith( "some, args" ) result = remboo.second( "yet", "others" ) assert result.endswith( "yet, others" ) # Try a global method; uses the same 'cli' socket, hence same server # # NOTE: We are *reusing* the same 'cli' 0MQ socket. This is OK, since all # client instances are in the same thread. However, since the 'client' # assumes authority for the socket, they will *both* try to clean it up # (close it). Generally, we'd suggest you have a separate socket for each # 'client' -- which would be required, anyway, if each is invoked from # within a separate application Thread. remgbl = zmqjsonrpc.client( socket=cli ) result = remgbl.sub( 42, 11 ) assert result == 31 # Various ways of failing to invoke nonexistent methods for rpc in [ remboo.nothere, # no such method remboo._hidden, # Hidden method (leading '_') remgbl.dir, # No such function ]: try: result = rpc() assert not "nonexistent method found: %s" % result except Exception, e: assert type(e) is zmqjsonrpc.Error assert "Method not found" in str(e) assert "-32601" in str(e)
def test_rpc_base_1_to_N(): global port N = 5 # Create 0MQ transport context = zmq.Context() cli = context.socket(zmq.REQ) cli.bind("tcp://*:%d" % (port)) # Create the test server and connect client to it thr = [] for i in xrange(N): svr = context.socket(zmq.XREP) svr.connect("tcp://localhost:%d" % (port)) svrthr = zmqjsonrpc.server_thread(root=globals(), socket=svr) svrthr.name = "Server-%d" % (i) svrthr.start() thr.append(svrthr) port += 1 # Create callable to method "first" and invoke; then "second" remboo = zmqjsonrpc.client(socket=cli, name="boo") result = remboo.first("some", "args") assert result.endswith("some, args") result = remboo.second("yet", "others") assert result.endswith("yet, others") # Try a global method; uses the same 'cli' socket, hence same server # # NOTE: We are *reusing* the same 'cli' 0MQ socket. This is OK, since all # client instances are in the same thread. However, since the 'client' # assumes authority for the socket, they will *both* try to clean it up # (close it). Generally, we'd suggest you have a separate socket for each # 'client' -- which would be required, anyway, if each is invoked from # within a separate application Thread. remgbl = zmqjsonrpc.client(socket=cli) result = remgbl.sub(42, 11) assert result == 31 # Various ways of failing to invoke nonexistent methods for rpc in [ remboo.nothere, # no such method remboo._hidden, # Hidden method (leading '_') remgbl.dir, # No such function ]: try: result = rpc() assert not "nonexistent method found: %s" % result except Exception, e: assert type(e) is zmqjsonrpc.Error assert "Method not found" in str(e) assert "-32601" in str(e)
#!/usr/bin/env python import zmq import zmqjsonrpc # Create 0MQ transport context = zmq.Context() socket = context.socket( zmq.REQ ) socket.connect( "tcp://localhost:55151" ) remote = zmqjsonrpc.client( socket=socket, name="boo" ) # Creates callable to method "first" and invokes; then "second" print "first: %s" % ( remote.first( "some", "args" )) print "second: %s" % ( remote.second( "yet", "others" )) # Clean up; destroy proxy (closes socket), terminate context del remote context.term()
#!/usr/bin/env python import zmq import zmqjsonrpc # Create 0MQ transport context = zmq.Context() socket = context.socket(zmq.REQ) socket.connect("tcp://localhost:55151") remote = zmqjsonrpc.client(socket=socket, name="boo") # Creates callable to method "first" and invokes; then "second" print "first: %s" % (remote.first("some", "args")) print "second: %s" % (remote.second("yet", "others")) # Clean up; destroy proxy (closes socket), terminate context del remote context.term()