最终决定使用Tornado作为WebSocket服务器,但我有一个关于它是如何实现的问题。
在学习了创建一个可用的服务器的基本教程之后,我得到了以下结论:
#!/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)来运行服务器。
拥有它们并不是什么大问题,但我只是想确保没有更好的方法来完成它们所做的事情(我还没有介绍这些模块)。
发布于 2012-05-11 06:58:08
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.
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.
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
我希望,这将涵盖您留下的模块。
发布于 2012-05-06 00:16:37
是的,它们是必需的,因为您正在使用来自您引用的每个模块/包的每个导入。如果您在源代码的顶部引用了某些内容,但在下面的任何代码中都不再使用它,那么您当然不需要它们,但在本例中,您使用的是导入。
https://stackoverflow.com/questions/5892895
复制相似问题