#!/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服务
最新推荐文章于 2025-01-08 13:45:15 发布
本文介绍了一个使用Python实现的JSON-RPC并发服务器程序。该程序利用了multiprocessing模块来处理高并发请求,并通过Pipe实现了父子进程间的数据传递。文章详细展示了如何设置HTTP服务器、定义请求处理器以及使用Pipe进行进程间通信。
4638

被折叠的 条评论
为什么被折叠?



