Gevent Pywsgi Server语言 - Multiprocessing?



下面的代码(取自这里:https://bitbucket.org/denis/gevent/src/6c710e8ae58b/examples/wsgiserver_ssl.py)实现了一个非常快的greenlet驱动的wsgi webserver:

#!/usr/bin/python
"""Secure WSGI server example based on gevent.pywsgi"""
from gevent import pywsgi

def hello_world(env, start_response):
    if env['PATH_INFO'] == '/':
        start_response('200 OK', [('Content-Type', 'text/html')])
        return ["<b>hello world</b>"]
    else:
        start_response('404 Not Found', [('Content-Type', 'text/html')])
        return ['<h1>Not Found</h1>']
print 'Serving on https://127.0.0.1:8443'
server = pywsgi.WSGIServer(('0.0.0.0', 8443), hello_world, keyfile='server.key', certfile='server.crt')
# to start the server asynchronously, call server.start()
# we use blocking serve_forever() here because we have no other jobs
server.serve_forever()

然而,这只能在一个核心上运行。您将如何修改它以利用多个过程?不去寻找一个涉及到金钱的答案,去寻找一个更简单的答案。

提示

下面是一个使用gevent和multiprocessing的代码示例,但是我仍然不能弄清楚如何使用WSGI(取自https://gist.github.com/1169975):

)使其工作。
import sys
from gevent import server
from gevent.baseserver import _tcp_listener
from gevent.monkey import patch_all; patch_all()
from multiprocessing import Process, current_process, cpu_count

def note(format, *args):
    sys.stderr.write('[%s]t%sn' % (current_process().name, format%args))
def echo(socket, address):
    print 'New connection from %s:%s' % address
    fileobj = socket.makefile()
    fileobj.write('Welcome to the echo server! Type quit to exit.rn')
    fileobj.write('In %srn' % current_process().name)
    fileobj.flush()
    while True:
        line = fileobj.readline()
        if not line:
            print "client disconnected"
            break
        if line.strip().lower() == 'quit':
            print "client quit"
            break
        fileobj.write(current_process().name + 't' + line)
        fileobj.flush()
        print "echoed", repr(line)
listener = _tcp_listener(('127.0.0.1', 8001))
def serve_forever(listener):
    note('starting server')
    server.StreamServer(listener, echo).serve_forever()
number_of_processes = 5
print 'Starting %s processes' % number_of_processes
for i in range(number_of_processes):
    Process(target=serve_forever, args=(listener,)).start()
serve_forever(listener)

那么为什么不以同样的方式来做呢?https://gist.github.com/1217855

import sys
from gevent import server
from gevent.baseserver import _tcp_listener
from gevent import pywsgi
from gevent.monkey import patch_all; patch_all()
from multiprocessing import Process, current_process, cpu_count
def hello_world(env, start_response):
    if env['PATH_INFO'] == '/':
        start_response('200 OK', [('Content-Type', 'text/html')])
        return ["<b>hello world</b>"]
    else:
        start_response('404 Not Found', [('Content-Type', 'text/html')])
        return ['<h1>Not Found</h1>']
listener = _tcp_listener(('127.0.0.1', 8001))
def serve_forever(listener):
    pywsgi.WSGIServer(listener, hello_world).serve_forever()
number_of_processes = 5
print 'Starting %s processes' % number_of_processes
for i in range(number_of_processes):
    Process(target=serve_forever, args=(listener,)).start()
serve_forever(listener)

还有一个与gevent一起工作的实用程序用于解决此问题,称为GIPC。

https://gehrcke.de/gipc/

优点是您可以在进程之间来回传递对象并进行通信。只是一个想法。

相关内容

  • 没有找到相关文章

最新更新