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

来源:互联网 发布:源码交易网站源码 编辑:程序博客网 时间:2024/06/05 09:24
#!/usr/bin/env python# coding: utf-8import pyjsonrpcfrom time import sleepimport 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-Serverhttp_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()
原创粉丝点击