- 浏览: 247372 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (127)
- vim (3)
- python (44)
- pymysql (1)
- mysql (9)
- macvim (1)
- erlang (3)
- twisted (0)
- tornado (5)
- django (7)
- postgresql (5)
- sql (1)
- java (7)
- tech (4)
- cache (1)
- lifestyle (3)
- html (1)
- ubuntu (2)
- rabbitmq (1)
- algorithm (8)
- Linux (4)
- Pythonista (1)
- thread (1)
- sort (6)
- 设计模式 (1)
- search (1)
- Unix (6)
- Socket (3)
- C (2)
- web (1)
- gc (1)
- php (10)
- macos (1)
最新评论
-
2057:
这个程序有bug。
查找算法学习之二分查找(Python版本)——BinarySearch -
dotjar:
NB
一个Python程序员的进化[转]
#!/usr/bin/env python #-*-encoding:utf-8-*- from tornado import httpserver from tornado import ioloop def handle_request(request): message = "You requested %s\n" % request.uri request.write("HTTP/1.1 200 OK\r\nContent-Length: %d\r\n\r\n%s" % ( len(message), message)) request.finish() http_server = httpserver.HTTPServer(handle_request)#在此打断点,来看下httpserver运行的过程。 http_server.listen(8888) ioloop.IOLoop.instance().start()
http_server = httpserver.HTTPServer(handle_request)
依然使用httpserver那篇的例子,继续调试ioloop的相关部分。
def add_sockets(self, sockets): if self.io_loop is None: self.io_loop = IOLoop.current() for sock in sockets: self._sockets[sock.fileno()] = sock add_accept_handler(sock, self._handle_connection, io_loop=self.io_loop)
@staticmethod def current(): #获取当前IOLoop是否存在实例,如果没有则初始化 current = getattr(IOLoop._current, "instance", None) if current is None: return IOLoop.instance() return current
#单例模式,确保IOLoop仅被实例化一次。 @staticmethod def instance(): if not hasattr(IOLoop, "_instance"): with IOLoop._instance_lock: if not hasattr(IOLoop, "_instance"): # New instance after double check IOLoop._instance = IOLoop() return IOLoop._instance
with IOLoop._instance_lock——>_instance_lock = threading.Lock()
@classmethod def configurable_default(cls): if hasattr(select, "epoll"): from tornado.platform.epoll import EPollIOLoop return EPollIOLoop if hasattr(select, "queue"): # Python 2.6+ on BSD or Mac from tornado.platform.kqueue import KQueueIOLoop return KQueueIOLoop from tornado.platform.select import SelectIOLoop return SelectIOLoop
判断支持哪种io复用模型,Linux epoll,BSD以及MacOS kqueue,否则返回select模型。
引用
For concrete implementations, see `tornado.platform.epoll.EPollIOLoop`
(Linux), `tornado.platform.kqueue.KQueueIOLoop` (BSD and Mac), or
`tornado.platform.select.SelectIOLoop` (all platforms)
由于当前系统为MacOS所以接下来跟踪的是KQueue
class KQueueIOLoop(PollIOLoop): #KQueueIOLoop继承的是PollIOLoop。 def initialize(self, **kwargs): super(KQueueIOLoop, self).initialize(impl=_KQueue(), **kwargs)
然后ioloop.IOLoop.instance().start() 开始事件循环
def start(self): if not logging.getLogger().handlers: # The IOLoop catches and logs exceptions, so it's # important that log output be visible. However, python's # default behavior for non-root loggers (prior to python # 3.2) is to print an unhelpful "no handlers could be # found" message rather than the actual log entry, so we # must explicitly configure logging if we've made it this # far without anything. logging.basicConfig() if self._stopped: self._stopped = False return old_current = getattr(IOLoop._current, "instance", None) IOLoop._current.instance = self self._thread_ident = thread.get_ident() self._running = True # signal.set_wakeup_fd closes a race condition in event loops: # a signal may arrive at the beginning of select/poll/etc # before it goes into its interruptible sleep, so the signal # will be consumed without waking the select. The solution is # for the (C, synchronous) signal handler to write to a pipe, # which will then be seen by select. # # In python's signal handling semantics, this only matters on the # main thread (fortunately, set_wakeup_fd only works on the main # thread and will raise a ValueError otherwise). # # If someone has already set a wakeup fd, we don't want to # disturb it. This is an issue for twisted, which does its # SIGCHILD processing in response to its own wakeup fd being # written to. As long as the wakeup fd is registered on the IOLoop, # the loop will still wake up and everything should work. old_wakeup_fd = None if hasattr(signal, 'set_wakeup_fd') and os.name == 'posix': # requires python 2.6+, unix. set_wakeup_fd exists but crashes # the python process on windows. try: old_wakeup_fd = signal.set_wakeup_fd(self._waker.write_fileno()) if old_wakeup_fd != -1: # Already set, restore previous value. This is a little racy, # but there's no clean get_wakeup_fd and in real use the # IOLoop is just started once at the beginning. signal.set_wakeup_fd(old_wakeup_fd) old_wakeup_fd = None except ValueError: # non-main thread pass while True: poll_timeout = 3600.0 # Prevent IO event starvation by delaying new callbacks # to the next iteration of the event loop. with self._callback_lock: callbacks = self._callbacks self._callbacks = [] for callback in callbacks: self._run_callback(callback) if self._timeouts: now = self.time() while self._timeouts: if self._timeouts[0].callback is None: # the timeout was cancelled heapq.heappop(self._timeouts) elif self._timeouts[0].deadline <= now: timeout = heapq.heappop(self._timeouts) self._run_callback(timeout.callback) else: seconds = self._timeouts[0].deadline - now poll_timeout = min(seconds, poll_timeout) break if self._callbacks: # If any callbacks or timeouts called add_callback, # we don't want to wait in poll() before we run them. poll_timeout = 0.0 if not self._running: break if self._blocking_signal_threshold is not None: # clear alarm so it doesn't fire while poll is waiting for # events. signal.setitimer(signal.ITIMER_REAL, 0, 0) try: event_pairs = self._impl.poll(poll_timeout) except Exception as e: # Depending on python version and IOLoop implementation, # different exception types may be thrown and there are # two ways EINTR might be signaled: # * e.errno == errno.EINTR # * e.args is like (errno.EINTR, 'Interrupted system call') if (getattr(e, 'errno', None) == errno.EINTR or (isinstance(getattr(e, 'args', None), tuple) and len(e.args) == 2 and e.args[0] == errno.EINTR)): continue else: raise if self._blocking_signal_threshold is not None: signal.setitimer(signal.ITIMER_REAL, self._blocking_signal_threshold, 0) # Pop one fd at a time from the set of pending fds and run # its handler. Since that handler may perform actions on # other file descriptors, there may be reentrant calls to # this IOLoop that update self._events self._events.update(event_pairs) while self._events: fd, events = self._events.popitem() try: self._handlers[fd](fd, events) except (OSError, IOError) as e: if e.args[0] == errno.EPIPE: # Happens when the client closes the connection pass else: app_log.error("Exception in I/O handler for fd %s", fd, exc_info=True) except Exception: app_log.error("Exception in I/O handler for fd %s", fd, exc_info=True) # reset the stopped flag so another start/stop pair can be issued self._stopped = False if self._blocking_signal_threshold is not None: signal.setitimer(signal.ITIMER_REAL, 0, 0) IOLoop._current.instance = old_current if old_wakeup_fd is not None: signal.set_wakeup_fd(old_wakeup_fd)
有关KQueue以及Epoll等陆续会单独整理总结。
打开http://127.0.0.1:8888/ 调试进入iostream。
Others:
1、errno
系统符号
引用
This module makes available standard errno system symbols. The value of each symbol is the corresponding integer value. The names and descriptions are borrowed from linux/include/errno.h, which should be pretty all-inclusive.
2、heapq
3、select
引用
This module provides access to the select() and poll() functions available in most operating systems, epoll() available on Linux 2.5+ and kqueue() available on most BSD. Note that on Windows, it only works for sockets; on other operating systems, it also works for other file types (in particular, on Unix, it works on pipes). It cannot be used on regular files to determine whether a file has grown since it was last read.
4、signal待整理
5、socket listen backlog参数以及setblocking()
6、getattr和setattr以及hasattr
Question:使用getattr的好处?
http://docs.python.org/2/library/functions.html#hasattr
http://docs.python.org/2/library/functions.html#getattr
http://docs.python.org/2/library/functions.html#setattr
参考资料:
http://docs.python.org/2/library/threading.html#lock-objects
http://effbot.org/zone/python-with-statement.htm
http://docs.python.org/2/reference/compound_stmts.html#the-with-statement
http://docs.python.org/2/library/signal.html
http://www.tornadoweb.org/documentation/ioloop.html
http://amyangfei.me/2013/01/27/tornado-source-analysis-1/
http://docs.python.org/2/library/select.html?highlight=select
http://stackoverflow.com/questions/7072701/whats-the-tornado-ioloop-and-tornados-workflow
http://golubenco.org/2009/09/19/understanding-the-code-inside-tornado-the-asynchronous-web-server-powering-friendfeed/
发表评论
-
macos 10.9.2 clang: error: unknown argument: '-mno-fused-madd' [-Wunused-command
2014-03-25 19:13 1761方法总是有的,当然需要你去寻找。 当然如果花费太多的时间在一件 ... -
PostgreSQL psycopg2:IndexError: tuple index out of range
2014-01-09 17:04 2231Postgresql psycopg2使用like查询的时候 ... -
Python 迭代器和生成器
2013-10-15 23:09 2851迭代器 迭代器只不过是一个实现迭代器协议的容器对象。它基于两个 ... -
Python时间模块
2013-10-15 23:03 3474time模块 时间模块中最常用的一个函数就是获取当前时间的函数 ... -
Python装饰器
2013-10-15 22:59 1571编写自定义装饰器有许多方法,但最简单和最容易理解的方法是编写一 ... -
python list
2013-10-15 22:56 1255简单总结以及整理如下: >>> dir( ... -
Python Excel
2013-09-10 17:21 977安装lib easy_install xlrd def ... -
排序算法学习(python版本)之堆排序(HeapSort)
2013-07-01 22:54 1999Contains: 堆排序以及堆排序的应用 堆排序(Heaps ... -
python range xrange
2013-06-25 23:30 1151引用Help on built-in function ran ... -
python class
2013-06-25 00:54 1829引用类是创建新对象类 ... -
AttributeError: 'module' object has no attribute 'SendCloud'
2013-06-05 11:46 7085网上查了下 意思是说你命名的文件名不能和lib重名,这样会导 ... -
python string
2013-05-07 23:44 2199如果这就是字符串,这本来就是字符串 首先看下字符串的方法 ... -
Python property
2013-03-29 19:56 0由于之前有总结过,可以参考http://2057.iteye. ... -
python tips
2013-03-28 23:57 8851、enum #!/usr/bin/env python ... -
python decorators
2013-03-28 23:36 1366Contains: 1、decorators 2、funct ... -
python closures
2013-03-28 22:09 1192Closure:如果在一个内部函数里,对在外部作用域(但不是在 ... -
Python map、filter,reduce介绍
2013-03-28 22:02 13131、filter(function,iterable) 引用C ... -
Python __new__ 、__init__、 __call__
2013-03-26 23:49 5352Contains: __new__: 创建对象时调用,返回当 ... -
Python socket简介
2013-03-25 23:42 2174自豪地使用dir和help. Python 2.7.2 ( ... -
Tornado httpserver 源码简析
2013-03-17 01:49 1792整个流程就是创建一个socket socket.socket ...
相关推荐
《深入剖析Tornado 1.2.0源码:揭示Web服务器的秘密》 Tornado,一个轻量级、高性能的Python Web服务器框架,以其异步非阻塞I/O模式著称,尤其适用于处理大量并发连接。本文将深入探讨Tornado 1.2.0版本的源码,...
tornado作为用户产品后台服务器核心框架 redis 保存session数据、短时间房源信息、地域信息等,保存页面缓存数据,提高服务器响应速度 采用前后端完全分离架构,采用ajax异步调用、json数据传输,使后端接口可以...
在"python+tornado开发的实例源码"中,我们可以学习到以下几个关键知识点: 1. **Python基础**:Python是一种高级编程语言,以其简洁明了的语法和丰富的标准库而闻名。在Tornado应用中,你需要了解Python的基本语法...
本文主要研究的是tornado 多进程模式的相关... tornado.ioloop.IOLoop.instance().start() 并且在listen中,将tornado启动为单进程模型。 所以要启动为多进程模型的方法为:摒弃listen方法 http_server = tornado.ht
8. **源码调试技巧**:介绍如何阅读和理解Tornado源码,以及使用Python调试工具进行源码调试的方法。 总之,这篇文章对于想深入了解Tornado框架,特别是其HTTP服务器实现的开发者来说,是一份宝贵的资源。通过源码...
- **tornado**: 包含 Tornado 源码的核心模块,如 httpserver.py(HTTP 服务器实现)、websocket.py(WebSocket 支持)、ioloop.py(I/O 循环)等。 - **test**: 测试目录,包含了对 Tornado 各个组件的单元测试,...
tornado.ioloop.IOLoop.current().start() ``` 在这个例子中,`IOLoop.current().start()` 启动了事件循环,等待处理到来的请求。 **三、Tornado 的长轮询与 WebSockets** Tornado 在实时 Web 应用中的优势在于...
Tornado官方翻译文档是...然后我们创建了一个应用实例,并将端口设置为监听8888,接着使用tornado.ioloop.IOLoop.current().start()启动了应用。这个例子尽管简单,但是体现了Tornado Web框架的基本结构和运行机制。
tornado.ioloop.IOLoop.current().start() ``` 这个例子定义了一个处理GET请求的处理器`MainHandler`,当访问根路径"/"时,返回"Hello, world"。 ### 四、Tornado的路由系统 Tornado的路由系统使用正则表达式来...
tornado作为web框架和异步网络库,代码量过多,因此在分析tornado源码时,可以选择一些比较重要的模块来阅读,方式:current.py,gen.py,tcpserver.py,httpserver.py,ioloop .py,iostream.py,web.py等 ...
这个特定的源码是基于Python编程语言实现的,利用了Tornado框架,一个轻量级且高效的异步网络库。下面将详细介绍这个系统可能包含的关键知识点和相关技术。 首先,Python是当今最流行的编程语言之一,尤其在数据...
Tornado采用了一个事件驱动的设计,利用了Python的`asyncio`库(在Python 3中)或者`tornado.ioloop`(在Python 2中)。IOLoop是Tornado的核心,它是一个事件循环,监控和调度所有异步操作。你可以注册回调函数,当...
`tornado.ioloop` 模块是 Tornado 的核心组件之一,它实现了事件驱动模型,负责调度各种 I/O 操作。开发者可以通过 `IOLoop.current().start()` 方法启动事件循环,也可以使用 `IOLoop.current().add_callback()` ...
tornado.ioloop.IOLoop.current().start() ``` 这个例子中,我们定义了一个名为MainHandler的处理类,它响应GET请求并返回"Hello, world"。然后,我们创建一个Application实例,设置路由,最后启动服务器监听8888...
from tornado.ioloop import IOLoop class MainHandler(RequestHandler): def get(self): self.write("Hello, Tornado!") if __name__ == "__main__": app = Application([ (r"/", MainHandler), ]) app....
tornado.ioloop.IOLoop.current().start() ``` 在这个例子中,我们创建了一个简单的Web服务器,监听8888端口,当访问根URL("/")时返回"Hello, world"。 总的来说,Tornado 1.2.1是一个强大且灵活的工具,适合...
《Tornado 2.2.2内核源码解析》 Tornado 2.2.2是VxWorks操作系统的一个重要版本,对于嵌入式开发人员来说,深入理解其内核源码对于优化系统性能、解决运行时问题以及进行定制化开发具有极大的价值。本文将对Tornado...
示例中没有使用Tornado的异步特性,但通过一个简单的“Hello, world”页面,演示了如何使用Tornado的IOLoop和Web模块来创建Web应用程序。开发者可以将此示例作为入门Tornado Web开发的起点。 文档还提到,与其他...
tornado.ioloop.IOLoop.current().start() ``` 这段代码定义了一个名为`MainHandler`的请求处理器,并将其绑定到根URL("/")。当收到GET请求时,它会返回"Hello, world"。 3. **WSGI兼容性**:虽然Tornado有自己...
Tornado 的安装可以通过 pip 完成,也可以手动下载源码包编译安装。在使用前,开发者需要理解 Tornado 的工作原理,主要是基于它的异步事件驱动模型,其中 ioloop(I/O 循环)是核心,它负责监听和调度各种 I/O 事件...