首页 > 其他 > 详细

理解tornado

时间:2015-12-10 12:44:52      阅读:227      评论:0      收藏:0      [点我收藏+]


nginx配合tornado

Tornado都采用的的单进程单线程异步IO的网络模型。
为什么要在tornado前面放nginx,原因在于,Python虽然有多线程,但是Python的解释器有GIL这点非常影响了Python和Tornado利用多
核的能力,所以只能通过多进程来利用多核。既然多进程,一般就需要在前端放置nginx做为负载均衡的反向代理,或是使用这些应用服务器
的wsgi模块来管理进程的生命周期等等。另外对于静态文件的服务,也是nginx之类的更具有优势。
 
计算密集型:多进程
IO密集型:多线程
 
能产生IO阻塞的情况很多,比如网络通讯、磁盘读写。当发生阻塞时,CPU是闲置的,此时如果就一个线程就没法处理其他事情了。所以对于
含有IO阻塞的环境,多线程可以提高CPU利用率。
 
Global interpreter lock (GIL) is a mechanism used in computer language interpreters to synchronize the execution of 
threads so that only one native thread can execute at a time.[1] An interpreter that uses GIL always allows exactly one
thread to execute at a time, even if run on a multi-core processor. Some popular interpreters that have GIL are CPython 
and Ruby MRI.
 
 

Benefits and drawbacks

increased speed of single-threaded programs (no necessity to acquire or release locks on all data structures separately), easy

integration of C libraries that usually are not thread-safe,    ease of implementation (having a single GIL is much simpler to

implement than a lock-free interpreter or one using fine-grained locks).

 

Use of a global interpreter lock in a language effectively limits the amount of parallelism reachable through concurrency of a

single interpreter process with multiple threads. If the process is almost purely made up of interpreted code and does not

make calls outside of the interpreter for long periods of time (which can release the lock on the GIL on that thread while it

processes), there is likely to be very little increase in speed when running the process on a multiprocessor machine. Due to

signaling with a CPU-bound thread, it can cause a significant slowdown, even on single processors.[2]

 

 

 

 
 

 

理解tornado

原文:http://www.cnblogs.com/yuyutianxia/p/5035250.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!