pyjsonrpc+multiprocessing实现可并发处理RPC服务

#!/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()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值