2

select有人在 Python 中通过Python stdlib测量 pyev 相对于标准绑定的速度和有用性吗?

使用 pyev over 有什么好处select

pyev 是由 C 扩展构建的,因此它不是可移植的解决方案。我尝试使用 PyPy 构建它,但开箱即用并没有成功。
所以我只是想知道是否值得使用它。

我知道gevent在它的 1.0 版本中使用 libev(在他们使用 libevent 之前)。他们真的需要吗?我不关心非事件循环功能(比如来自 libevent 的 dns)。

4

1 回答 1

2

Python 的 select 模块只是 select()、poll() 和 epoll() 系统调用的包装,而 libev 和 libevent 实现了一个事件循环。事件循环管理观察者和计时器,将待处理的事件排队,调用你的回调等。

如果您想将 libev/libevent 与 Python 对应项进行比较,则需要将它们与 twisted 的 reactor 和 tornado 的 IOLoop 进行比较。

来自libev 文档

以下是 ev_run 所做的血淋淋的细节(这是为了您的理解,不保证在未来的版本中事情会完全像这样工作):

   - Increment loop depth.
   - Reset the ev_break status.
   - Before the first iteration, call any pending watchers.
   LOOP:
   - If EVFLAG_FORKCHECK was used, check for a fork.
   - If a fork was detected (by any means), queue and call all fork watchers.
   - Queue and call all prepare watchers.
   - If ev_break was called, goto FINISH.
   - If we have been forked, detach and recreate the kernel state
     as to not disturb the other process.
   - Update the kernel state with all outstanding changes.
   - Update the "event loop time" (ev_now ()).
   - Calculate for how long to sleep or block, if at all
     (active idle watchers, EVRUN_NOWAIT or not having
     any active watchers at all will result in not sleeping).
   - Sleep if the I/O and timer collect interval say so.
   - Increment loop iteration counter.
   - Block the process, waiting for any events.
   - Queue all outstanding I/O (fd) events.
   - Update the "event loop time" (ev_now ()), and do time jump adjustments.
   - Queue all expired timers.
   - Queue all expired periodics.
   - Queue all idle watchers with priority higher than that of pending events.
   - Queue all check watchers.
   - Call all queued watchers in reverse order (i.e. check watchers first).
     Signals and child watchers are implemented as I/O watchers, and will
     be handled here by queueing them when their watcher gets executed.
   - If ev_break has been called, or EVRUN_ONCE or EVRUN_NOWAIT
     were used, or there are no active watchers, goto FINISH, otherwise
     continue with step LOOP.
   FINISH:
   - Reset the ev_break status iff it was EVBREAK_ONE.
   - Decrement the loop depth.
   - Return.
于 2012-05-17T23:17:47.917 回答