2015-11-16 122 views
0

我遇到麻烦与doctest一起使用扭曲。我想open a file像这样:使用扭曲doctest

from __future__ import print_function 

from twisted.internet.defer import Deferred 
from twisted.internet.fdesc import readFromFD, setNonBlocking 
from twisted.internet.task import react 


def asyncFunc(filename): 
    """Description. 

    Examples: 
     >>> def run(reactor, filename): 
     ...  d = asyncFunc(filename) 
     ...  return d.addCallback(print) 
     >>> 
     >>> try: 
     ...  react(run, ['hello.txt']) 
     ... except SystemExit: 
     ...  pass 
     hello world 
     <BLANKLINE> 
    """ 
    with open(filename) as f: 
     d = Deferred() 
     fd = f.fileno() 
     setNonBlocking(fd) 
     readFromFD(fd, d.callback) 
     return d 

它工作正常只是一个测试,但不止一个,我收到了ReactorNotRestartable错误。

def anotherAsyncFunc(filename): 
    """Description. 

    Examples: 
     >>> def run(reactor, filename): 
     ...  d = anotherAsyncFunc(filename) 
     ...  return d.addCallback(print) 
     >>> 
     >>> try: 
     ...  react(run, ['hello.txt']) 
     ... except SystemExit: 
     ...  pass 
     hello world 
     <BLANKLINE> 
    """ 
    with open(filename) as f: 
     d = Deferred() 
     fd = f.fileno() 
     setNonBlocking(fd) 
     readFromFD(fd, d.callback) 
     return d 

然后我读到MemoryReactor,并试图此:

def anotherAsyncFunc(filename): 
    """Description. 

    Examples: 
     >>> from twisted.test.proto_helpers import MemoryReactor 
     >>> reactor = MemoryReactor() 
     >>> 
     >>> def run(reactor, filename): 
     ...  d = anotherAsyncFunc(filename) 
     ...  return d.addCallback(print) 
     >>> 
     >>> try: 
     ...  react(run, ['hello.txt'], reactor) 
     ... except SystemExit: 
     ...  pass 
     hello world 
     <BLANKLINE> 
    """ 
    with open(filename) as f: 
     d = Deferred() 
     fd = f.fileno() 
     setNonBlocking(fd) 
     readFromFD(fd, d.callback) 
     return d 

但是,这给了我一个'MemoryReactor' object has no attribute 'addSystemEventTrigger'AttributeError。任何想法的正确方法来做到这一点?

回答

0

您已正确识别MemoryReactor未提供界面IReactorCore。正确的做法是将IReactorCore的实现贡献给MemoryReactor,这样第二个示例将按需要工作。请打开一张票,并做到这一点:)。

+0

感谢@ glyph的帮助。我对整个界面概念不熟悉,所以请你指点一下现有的票或我可以遵循的示例作为指导? – reubano

+0

我想不出具体的票,但它并不复杂; ['IReactorCore'](https://twistedmatrix.com/documents/15.4.0/api/twisted.internet.interfaces.IReactorCore.html)有一堆方法和属性,只需将这些属性添加到'MemoryReactor'(和然后将IReactorCore添加到它的'@ implements'行)。接口只是对特定对象必须具有的特征的文档化描述。 – Glyph

+0

gotcha ....他们是否真的需要做任何事情,或者我可以只为他们使用'pass'? – reubano