#!/usr/bin/env python
# coding: utf-8
import pyjsonrpc
from time import sleep
import multiprocessing
#将结果通过PIPE发送给主进程
def work(pipe , a , b ) :
i = 0
while True :
i += 1 ;
if i > 1000000000 :
break
pipe.send(a + b)
class RequestHandler(pyjsonrpc.HttpRequestHandler):
@pyjsonrpc.rpcmethod
def add(self, a, b):
#多进程中一对一的通信工具,不同于QUEUE
pipe = multiprocessing.Pipe()
#print pipe
# Pass an end of the pipe to process 1
#将业务逻辑推到另一个进程中处理
p1 = multiprocessing.Process(target=work, args=(pipe[0],a , b))
p1.start()
#等待子进程返回
p1.join()
#返回子进程发回来的结果
return pipe[1].recv()
# Threading HTTP-Server
http_server = pyjsonrpc.ThreadingHttpServer(
server_address = ('192.168.56.100', 8080),
RequestHandlerClass = RequestHandler
)
print "Starting HTTP server ..."
print "URL: http://localhost:8080"
http_server.serve_forever()
pyjsonrpc+multiprocessing实现可并发处理RPC服务
最新推荐文章于 2023-10-11 22:22:46 发布