RPYC (Remote Python Call) provides an excellent base platform for distributed computing environments. Use RPYC to write C/s structure program, do not have to consider the old socket programming, now only to write a simple 3, 5 lines of code to complete the previous thousands of lines of code functions.
A simple example to explain:
Service side:
[Python]View Plain Copy # Coding:utf-8 from RPYC import Service from Rpyc.utils.serverimport Threadedserver class Test Service (Service): # for the server, only the "Exposed_" method can be invoked by the client, so the method to provide to the client must be "Exposed_" defexposed_test (self, num ): Return1+num sr = Threadedserver (TESTRPYC, port=9999, Auto_register=false) Sr.start ()
Client:
[Python]View Plain Copy # coding:utf-8 import RPYC # parameters are mainly host, Port Conn =rpyc.connect (' localhost ', 9999) # test is a service-side The way to start with "Exposed_" Cresult =conn.root.test () conn.close () print Cresult
Note: For return value Cresult
1, if Cresult is a number or string, then after Conn.close (), you can use the Cresult value
2, if Cresult is other types of data, you Conn.close () after the Cresult is empty (this is because for other types of return value, the server returns the Rpyc.netref package Nobj, when access to Nobj, it is connected to the server side, Value, and return to the client, you close after you can not connect to the service side, so it is best to the service side of the calculation results processed, converted to a number or string (for large dictionaries, can be converted with JSON, and then the client turned around OK)
Note: There are three kinds of classic RPYC service modes:
Classic Server
The classic server, implemented in rpyc/servers/classic_server.py, are an executable script. When running it, your can specify command line options to control how it behaves:
Option Description
-M or--mode one of threaded, forking, or stdio. The default is threaded
-P or--port the TCP port on which the server listens. Applicable only for threaded and forking modes. The default is 18812
-Q or--quiet does not display any logs, except for errors. Default is verbose
Modes:
In the threaded mode, the server creates a thread to serve each client. This is usually the basic setup required. Supported on Windows, Linux, and most the other flavors of Unix.
In the forking mode, the server forks a child process to serve each client. Not supported on Windows.
The STD mode is useful for inetd invocation. In this mode, the server reads from STDIN and writes to StdOut instead of operating on a socket.
The default is threading mode, in which the RPYC service creates a thread service for each client
Other RPYC details Reader Web document: http://rpyc.wikidot.com/
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.