gunicorn geventworker 解析
来源:互联网 发布:综合体检数据 编辑:程序博客网 时间:2024/05/17 01:59
本文介绍其中一种asyncworker:GeventWorker。类图如下:
1 def init_process(self): 2 # monkey patch here 3 self.patch() 4 5 # reinit the hub 6 from gevent import hub 7 hub.reinit() 8 9 # then initialize the process10 super(GeventWorker, self).init_process()
首先是monkey-patch,这个是使用gevent所必须的的;然后调用hub.reinit,这个在fork子进程之后必须调用;最后调用基类的init_process。关于gevent hub的reinit,在gevent源码的doc里写的很清楚:
This should be called *immediately* after :func:`os.fork` in the child process. This is done automatically by :func:`gevent.os.fork` or if the :mod:`os` module has beenmonkey-patched. If this function is not called in a forked process, symptoms may include hanging of functions like :func:`socket.getaddrinfo`, and the hub's threadpool is unlikelyto work.
前面也提到了init_process会带用到run方法,进入循环,我们看看GeventWorker.run:
1 def run(self): 2 servers = [] 3 ssl_args = {} 4 5 if self.cfg.is_ssl: 6 ssl_args = dict(server_side=True, **self.cfg.ssl_options) 7 8 for s in self.sockets: 9 s.setblocking(1)10 pool = Pool(self.worker_connections) # Pool是Gevent.Pool11 if self.server_class is not None:12 environ = base_environ(self.cfg)13 environ.update({14 "wsgi.multithread": True,15 "SERVER_SOFTWARE": VERSION,16 })17 server = self.server_class(18 s, application=self.wsgi, spawn=pool, log=self.log,19 handler_class=self.wsgi_handler, environ=environ,20 **ssl_args)21 else:22 hfun = partial(self.handle, s) # self.handle为请求到达时候的处理函数23 server = StreamServer(s, handle=hfun, spawn=pool, **ssl_args)24 25 server.start()26 servers.append(server)27 28 while self.alive: # 每隔1s告知Arbiter自己还活着29 self.notify() 30 gevent.sleep(1.0) # 切换回hub
逻辑也比较简单,首先是建立TCP Server(gevent.StreamServer),并且传入tcp连接到达时得处理函数self.handle, 然后TCP Server开始工作(server.start), 关于gevent具体怎么在多个协程之间调度,可以参加之前的文章。最后的while循环(28行)用于向Arbiter发心跳,并通过gevent.sleep立即切回hub。再来看看上述代码的第10行:pool = Pool(self.worker_connections) ,这个worker_connections属性来自gunicorn的配置,默认为1000,表明单个worker可以维持的连接数目。然后gevent.Pool的作用可以再gevent tutorial查看,简单来说,就是规定了可以在单个线程内可以并发的greenlet.greenlet的数目。当TCP server接受到请求之后,调用self.handle处理请求,源代码在基类AsyncWorker,实现基本同SyncWorker。
- gunicorn geventworker 解析
- gunicorn Arbiter 源码解析
- gunicorn Arbiter 源码解析
- gunicorn syncworker 源码解析
- gunicorn Arbiter 源码解析
- gunicorn配置文件
- gunicorn timeout
- gunicorn 简介
- gunicorn常用命令
- nginx+gunicorn运行django
- nginx+gunicorn+django
- nginx+gunicorn+bottle
- gunicorn部署django实例
- 【Django】 gunicorn部署纪要
- Gunicorn启动Thrift服务
- Nginx + Gunicorn(+gevent) + Django
- Flask + Gunicorn + Nginx 部署
- gunicorn + nginx + django
- Fel异常Check that your class path includes tools
- 23.struts2_输入验证
- (83)蓝图编辑器默认值选卡
- JavaFX实现图片显示,文本输入,表格显示
- This action could not be completed
- gunicorn geventworker 解析
- Linux下动态链接库的制作与使用
- ngrok 实现内网映射
- 29.七-27拆分的testsub
- Python2.7 的IDLE启动无反应
- SQL中 decode()函数简介
- Apache与Nginx的优缺点比较
- 续流二极管
- AAChartKit 一款功能极其强大的开源图表组件库