gpt4 book ai didi

python - Gevent Pywsgi 服务器 - 多处理?

转载 作者:太空宇宙 更新时间:2023-11-03 15:25:42 27 4
gpt4 key购买 nike

以下代码(取自此处: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)

最佳答案

那为什么不用同样的方法呢? 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)

关于python - Gevent Pywsgi 服务器 - 多处理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7407868/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com