【问题标题】:Tornado + kevent on Mac OSXMac OSX 上的 Tornado + kevent
【发布时间】:2014-12-05 00:57:37
【问题描述】:

我最近在 Mac OSX Yosemite 上运行 tornado 时遇到了问题。当向龙卷风服务器发出请求时,我得到以下回溯:

ERROR:tornado.general:Uncaught exception
Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/tornado/http1connection.py", line 674, in _server_request_loop
    ret = yield conn.read_response(request_delegate)
  File "/Library/Python/2.7/site-packages/tornado/gen.py", line 628, in run
    value = future.result()
  File "/Library/Python/2.7/site-packages/tornado/concurrent.py", line 109, in result
    raise_exc_info(self._exc_info)
  File "/Library/Python/2.7/site-packages/tornado/gen.py", line 175, in wrapper
    yielded = next(result)
  File "/Library/Python/2.7/site-packages/tornado/http1connection.py", line 157, in _read_message
    max_bytes=self.params.max_header_size)
  File "/Library/Python/2.7/site-packages/tornado/iostream.py", line 227, in read_until_regex
    self._try_inline_read()
  File "/Library/Python/2.7/site-packages/tornado/iostream.py", line 673, in _try_inline_read
    self._add_io_state(ioloop.IOLoop.READ)
  File "/Library/Python/2.7/site-packages/tornado/iostream.py", line 881, in _add_io_state
    self.fileno(), self._handle_events, self._state)
  File "/Library/Python/2.7/site-packages/tornado/ioloop.py", line 677, in add_handler
    self._impl.register(fd, events | self.ERROR)
  File "/Library/Python/2.7/site-packages/tornado/platform/kqueue.py", line 41, in register
    self._control(fd, events, select.KQ_EV_ADD)
  File "/Library/Python/2.7/site-packages/tornado/platform/kqueue.py", line 59, in _control
    kevents.append(select.kevent(
AttributeError: 'module' object has no attribute 'kevent'

最后一行是最重要的。我相当肯定这只是因为缺少 brew 包,但我不知道我会缺少什么。

更神奇的是,我可以导入 select 并看到 kevent 函数存在。

Python 2.7.6 (default, Sep  9 2014, 15:04:36)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import select
>>> select.kevent
<type 'select.kevent'>

【问题讨论】:

  • 嗯,这很奇怪。您必须拥有select.kqueue 才能达到这一点,因此您也应该拥有select.keventimport select; print(dir(select)) 为您打印什么?此外,您提到了 brew,但 /Library 中的这些路径是 OSX 系统 python,而不是 brew。也许 brew 和非 brew 安装之间有些混淆?
  • 我在我的问题中添加了更多内容,这进一步增加了混乱。我认为这是一个合理的假设,系统和 brew 安装已经纠缠不清——我会调查一下。
  • 我忘记将我的默认python切换到brew版本而不是系统版本,但是切换似乎仍然没有解决问题。
  • which python 返回usr/local/bin/python
  • 即使which python/usr/local/bin/python,堆栈跟踪中的路径也是/Library?这听起来不对,虽然我使用 macports 而不是 brew 所以在这里我无法提供更多帮助。 (我发现 macports 比 homebrew 对 python 的支持更好,FWIW)

标签: python macos tornado kevent


【解决方案1】:

所以我想出了一个临时解决方案,即使是打电话给我也感到有点内疚,但无论如何:

1) 打开 /usr/local/lib/python2.7/site-packages/tornado/platform/kqueue.py (或类似的)。 2) 在import select 之后立即添加以下行:

cached_kevent = select.kevent

3) 替换以下行:

kevents.append(select.kevent(
    fd, filter=select.KQ_FILTER_READ, flags=flags))

使用此修改版本:

try:
    kevents.append(select.kevent(
        fd, filter=select.KQ_FILTER_READ, flags=flags))
except AttributeError:
    kevents.append(cached_kevent(
       fd, filter=select.KQ_FILTER_READ, flags=flags))

任何更好的答案将不胜感激。

【讨论】:

  • 有点晚了,但我们刚刚收到了关于 IPython 的类似问题的报告。您的代码是否有可能导入 gevent(或使用 gevent 的东西)? Gevent 出现在remove select.kevent
  • 是的,gevent 确实是在我相信的相同上下文中导入的。
  • 这似乎是一个值得商榷的设计决定,但是……我并不感到惊讶。
猜你喜欢
  • 2011-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-25
  • 2011-06-02
  • 1970-01-01
  • 2013-10-29
  • 2018-10-27
相关资源
最近更新 更多