首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Tornado WebSocket问题

Tornado WebSocket问题
EN

Stack Overflow用户
提问于 2011-05-05 13:05:27
回答 2查看 3.4K关注 0票数 1

最终决定使用Tornado作为WebSocket服务器,但我有一个关于它是如何实现的问题。

在学习了创建一个可用的服务器的基本教程之后,我得到了以下结论:

代码语言:javascript
复制
#!/usr/bin/env python

from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop
from tornado.web import Application
from tornado.websocket import WebSocketHandler

class Handler(WebSocketHandler):
        def open(self):
            print "New connection opened."

        def on_message(self, message):
                print message


        def on_close(self):
                print "Connection closed."

print "Server started."
HTTPServer(Application([("/", Handler)])).listen(1024)
IOLoop.instance().start()

它工作得很好,但我想知道是否真的需要其他模块(tornado.httpserver、tornado.ioloop和tornado.web)来运行服务器。

拥有它们并不是什么大问题,但我只是想确保没有更好的方法来完成它们所做的事情(我还没有介绍这些模块)。

EN

回答 2

Stack Overflow用户

发布于 2012-05-11 06:58:08

  • tornado.httpserver :

代码语言:javascript
复制
1. A **non-blocking, single-threaded** HTTP server.
2. Typical applications have little direct interaction with the HTTPServer class.
3. HTTPServer is a very basic connection handler. Beyond parsing the HTTP request body and headers, the **only HTTP semantics** implemented in HTTPServer is HTTP/1.1 keep-alive connections.

  • tornado.ioloop :

代码语言:javascript
复制
1. An I/O event loop for **non-blocking sockets**.
2. So, the ioloop can be used for **setting the time-out** of the response.
3. In general, methods on RequestHandler and elsewhere in tornado are **not thread-safe**. In particular, methods such as write(), finish(), and flush() must only be called from the main thread. If you use multiple threads it is important to use **IOLoop**.add\_callback to transfer control back to the main thread before finishing the request.

  • tornado.web :

代码语言:javascript
复制
1. Provides **RequestHandler** and **Application** classes
2. Helps with additional tools and optimizations to take **advantage of the Tornado non-blocking web server** and tools.
3. So, these are the provisions by this module : 
    - Entry points :    Hook for subclass initialization.
    - Input
    - Output
    - Cookies

我希望,这将涵盖您留下的模块。

票数 6
EN

Stack Overflow用户

发布于 2012-05-06 00:16:37

是的,它们是必需的,因为您正在使用来自您引用的每个模块/包的每个导入。如果您在源代码的顶部引用了某些内容,但在下面的任何代码中都不再使用它,那么您当然不需要它们,但在本例中,您使用的是导入。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5892895

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档