epye with pyev或从Python中的stdlib中选择?

时间:2012-05-17 21:29:02

标签: python gevent libevent libev

有人在Python中测量了pyev的速度和有用性,而不是来自Python stdlib的select的标准绑定吗?

使用pyev比select有什么好处?

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

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

1 个答案:

答案 0 :(得分:2)

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

如果你想将libev / libevent与Python对应物进行比较,你需要将它们与twisted的反应堆和龙卷风的IOLoop进行比较。

来自libev documentation

  

以下是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.