简体   繁体   English

非阻塞http服务器,java nio,python tornado eventlet

[英]Non-blocking http server , java nio , python tornado eventlet

Hi I am trying to Understand if tornado/eventlet based http sever are better than threaded sever. 嗨,我想了解基于tornado / eventlet的http服务器是否优于线程服务器。 While goggling the subject I am seeing that these are single thread event base server which run a single handler function after select/poll/epoll on socket. 在看到主题时,我看到这些是单线程事件基本服务器,它在套接字上的select / poll / epoll之后运行单个处理函数。

  • My first question is that is this tornado/eventlet similar to nio library in java and is a java nio server non-blocking and fast. 我的第一个问题是这个龙卷风/ eventlet类似于java中的nio库,是一个非阻塞且快速的java nio服务器。
  • My second question is that since this event based Server are single thread If one connection blocks on file io or solw client will it hang the entire server 我的第二个问题是,因为这个基于事件的服务器是单线程如果一个连接阻塞文件io或solw客户端将挂起整个服务器
  • My third question is that what is the trade off , if non blocking server is fast why isn't it is more common than apache 我的第三个问题是什么是权衡,如果非阻塞服务器速度快,为什么它不比apache更常见

These questions are related and I would apprecite as I am not understanding these issues correctly 这些问题是相关的,我会很感激,因为我没有正确理解这些问题

Thanks 谢谢

Nonblocking servers are the best choice provided all your libraries provides nonblocking apis. 如果所有库都提供非阻塞apis,则非阻塞服务器是最佳选择。 As mentioned in your second question if a library blocks (eg database lib making a blocking call), the entire process/thread blocks and the system hangs. 如第二个问题所述,如果库阻塞(例如数据库lib进行阻塞调用),则整个进程/线程块和系统挂起。 Not all of the libraries available are asynchronous which makes it difficult to use tornado/eventlet for all usecases. 并非所有可用的库都是异步的,因此很难对所有用例使用tornado / eventlet。 Also in a multi-core box multiple instances of nonblocking servers needs to be started to use the box capacity completly. 此外,在多核盒中,需要启动多个非阻塞服务器实例以完全使用盒容量。

Tornado/Event servers are similar to java nio based servers. Tornado / Event服务器类似于基于java nio的服务器。 There is one conceptual difference between a Tornado and Eventlet. Tornado和Eventlet之间存在一个概念上的区别。 Tornado follows a reactor pattern where the single process waits for IO(socket) events and dispatches them to appropriate handlers. Tornado遵循反应器模式,其中单个进程等待IO(套接字)事件并将它们分派给适当的处理程序。 If handlers are nonblocking, best performance can be expected. 如果处理程序是非阻塞的,则可以获得最佳性能。 Typically code written for these frameworks consists of a series of callbacks making it a bit less readable than a synchronous server .Java NIO servers comes under this category. 通常,为这些框架编写的代码包含一系列回调,使其比同步服务器的可读性低一些.Java NIO服务器属于此类别。

Eventlet performs the same task but with a cleaner interface. Eventlet执行相同的任务,但界面更清晰。 Code can be written as in the case of synchronous server without using callbacks. 代码可以像同步服务器一样编写而不使用回调。 When an IO is encountered, eventlet schedules another userspace process(not right terminology). 遇到IO时,eventlet会调度另一个用户空间进程(不是正确的术语)。

Apache webapps are more popular that these because of few reasons 由于几个原因,Apache webapps更受欢迎

  • It is relatively easy to write synchronous code 编写同步代码相对容易
  • Not all required libraries are asynchronous. 并非所有必需的库都是异步的。

But for writing a chat application which handles lots of connections a multi-threaded server will not scale. 但是,对于编写处理大量连接的聊天应用程序,多线程服务器将无法扩展。 You have to use async frameworks like twisted/event/Java NIO. 您必须使用twisted / event / Java NIO等异步框架。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM