扭曲 运行 异步示例并在 Python 提示符下建立连接?

twisted run asynchronous examples and make connections at Python prompt?

ldaptor 项目在交互式 Python 提示符处有一些基于 Twisted 的示例代码。但是,在某一点 [1],如果您实际上尝试按照提示进行操作,则该示例会中断,因为一个函数已被弃用 -- twisted.trial.util.deferredResult()。有趣的是,这显然让反应堆运行、建立的连接和延迟结果(LDAPClient 协议实例)是returned,可以在交互式Python 解释器中进行操作.

我的问题是,是否有任何现代方法可以为了举例或只是为了试验而做这类事情?我可以在提示符下做这样的事情:

>>> from ldaptor.protocols.ldap.ldapclient import LDAPClient
>>> from twisted.internet import reactor
>>> from twisted.internet.endpoints import clientFromString, connectProtocol
>>> e = clientFromString(reactor, "tcp:host=localhost:port=10389")
>>> e
<twisted.internet.endpoints.TCP4ClientEndpoint at 0xb452e0c>
>>> d = connectProtocol(e, LDAPClient)
>>> d
<Deferred at 0xb34656c>

但我想不出任何方法来 运行 校长将 return 延迟结果发送到交互式提示。可能吗?钩针项目会有帮助吗?

[1] https://ldaptor.readthedocs.org/en/latest/addressbook-example.html#searching

你可以试试:

  • python -m twisted.conch.stdio
  • pip install bpython urwid; bpython-urwid --reactor select

其中每一个都会给你一个增强的 Python 提示,背景中有一个 Twisted 反应器 运行。前者原生支持 Deferreds,但后者功能更丰富。