我的 django 项目之一是使用ansible (gunicorn & nginx)部署的。下面是gunicorn配置:
bind = '127.0.0.1:8001'
backlog = 2048
workers = 8
worker_class = 'sync'
worker_connections = 1000
timeout = 300
keepalive = 2
spew = False
daemon = False
pidfile = None
umask = 0
user = None
group = None
tmp_upload_dir = None
loglevel = 'info'
errorlog = '/var/log/error.log'
accesslog = '/var/log/access.log'
proc_name = None
def pre_fork(server, worker):
pass
def pre_exec(server):
server.log.info("Forked child, re-executing.")
def when_ready(server):
server.log.info("Server is ready. Spawning workers")
def worker_int(worker):
worker.log.info("worker received INT or QUIT signal")
## get traceback info
import threading, sys, traceback
id2name = dict([(th.ident, th.name) for th in threading.enumerate()])
code = []
for threadId, stack in sys._current_frames().items():
code.append("\n# Thread: %s(%d)" % (id2name.get(threadId,""),
threadId))
for filename, lineno, name, line in traceback.extract_stack(stack):
code.append('File: "%s", line %d, in %s' % (filename,
lineno, name))
if line:
code.append(" %s" % (line.strip()))
worker.log.debug("\n".join(code))
def worker_abort(worker):
worker.log.info("worker received SIGABRT signal")
工作进程同步运行。我希望它们同时运行,因为我在这台服务器上每分钟有很多请求。在研究中,我发现我可以使用 python 库,比如eventlet
使用greenthreads进行并发。为此,我需要将 worker_class 更改为 eventlet:
worker_class = eventlet
但现在我一无所知。我不知道如何为这个项目实现异步绿色线程。可能这是一个愚蠢的问题,但我真的需要一些帮助。
提前致谢。