运行 在另一个线程中运行
Run function in another thread
假设有两个 运行ning 代码:script1 和 script2。
我希望 script2 能够 运行 script1 中的函数。
script1 将是某种运行“永远”的后台进程。
重点是能够为后台进程创建 API,例如服务器。
不干净的方法是让文件从 script2 传输命令。然后 script1 将使用 exec()
执行它。但是,我想使用模块或更清洁的东西,因为这样我就可以输出 类 而不仅仅是文本。
编辑:示例:
脚本 1:
def dosomething(args):
# do something
return information
while True:
# Do something in a loop
脚本 2:
# "import" the background process
print(backgroundprocess.dosomething(["hello", (1, 2, 3)]))
执行过程如下所示:
- 运行 脚本 1
- 运行 脚本 2 并行 window
总结
XMLRPC 模块就是为此目的而设计的。
文档包括一个针对服务器 (script1) 和客户端 (script2) 的示例。
服务器示例
from xmlrpc.server import SimpleXMLRPCServer
from xmlrpc.server import SimpleXMLRPCRequestHandler
class RequestHandler(SimpleXMLRPCRequestHandler):
rpc_paths = ('/RPC2',)
# Create server
with SimpleXMLRPCServer(('localhost', 8000),
requestHandler=RequestHandler) as server:
server.register_introspection_functions()
# Register pow() function; this will use the value of
# pow.__name__ as the name, which is just 'pow'.
server.register_function(pow)
# Register a function under a different name
def adder_function(x, y):
return x + y
server.register_function(adder_function, 'add')
# Register an instance; all the methods of the instance are
# published as XML-RPC methods (in this case, just 'mul').
class MyFuncs:
def mul(self, x, y):
return x * y
server.register_instance(MyFuncs())
# Run the server's main loop
server.serve_forever()
客户端示例
import xmlrpc.client
s = xmlrpc.client.ServerProxy('http://localhost:8000')
print(s.pow(2,3)) # Returns 2**3 = 8
print(s.add(2,3)) # Returns 5
print(s.mul(5,2)) # Returns 5*2 = 10
# Print list of available methods
print(s.system.listMethods())
假设有两个 运行ning 代码:script1 和 script2。 我希望 script2 能够 运行 script1 中的函数。 script1 将是某种运行“永远”的后台进程。
重点是能够为后台进程创建 API,例如服务器。
不干净的方法是让文件从 script2 传输命令。然后 script1 将使用 exec()
执行它。但是,我想使用模块或更清洁的东西,因为这样我就可以输出 类 而不仅仅是文本。
编辑:示例:
脚本 1:
def dosomething(args):
# do something
return information
while True:
# Do something in a loop
脚本 2:
# "import" the background process
print(backgroundprocess.dosomething(["hello", (1, 2, 3)]))
执行过程如下所示:
- 运行 脚本 1
- 运行 脚本 2 并行 window
总结
XMLRPC 模块就是为此目的而设计的。
文档包括一个针对服务器 (script1) 和客户端 (script2) 的示例。
服务器示例
from xmlrpc.server import SimpleXMLRPCServer
from xmlrpc.server import SimpleXMLRPCRequestHandler
class RequestHandler(SimpleXMLRPCRequestHandler):
rpc_paths = ('/RPC2',)
# Create server
with SimpleXMLRPCServer(('localhost', 8000),
requestHandler=RequestHandler) as server:
server.register_introspection_functions()
# Register pow() function; this will use the value of
# pow.__name__ as the name, which is just 'pow'.
server.register_function(pow)
# Register a function under a different name
def adder_function(x, y):
return x + y
server.register_function(adder_function, 'add')
# Register an instance; all the methods of the instance are
# published as XML-RPC methods (in this case, just 'mul').
class MyFuncs:
def mul(self, x, y):
return x * y
server.register_instance(MyFuncs())
# Run the server's main loop
server.serve_forever()
客户端示例
import xmlrpc.client
s = xmlrpc.client.ServerProxy('http://localhost:8000')
print(s.pow(2,3)) # Returns 2**3 = 8
print(s.add(2,3)) # Returns 5
print(s.mul(5,2)) # Returns 5*2 = 10
# Print list of available methods
print(s.system.listMethods())