以下代码(取自此处:https : //bitbucket.org/denis/gevent/src/6c710e8ae58b/examples/wsgiserver_ssl.py)实现了一个极快的由greenlet驱动的wsgi网络服务器:
#!/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()
但是,这只在一个核心上运行。您将如何修改它以利用多个进程?不是在寻找涉及 gunicorn 的答案,而是在寻找更简单的东西。
暗示
这是一个使用 gevent 和多处理的代码示例,但我仍然无法弄清楚如何使用 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%s\n' % (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.\r\n')
fileobj.write('In %s\r\n' % 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)