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
。任何想法的正确方法来做到这一点?
感谢@ glyph的帮助。我对整个界面概念不熟悉,所以请你指点一下现有的票或我可以遵循的示例作为指导? – reubano
我想不出具体的票,但它并不复杂; ['IReactorCore'](https://twistedmatrix.com/documents/15.4.0/api/twisted.internet.interfaces.IReactorCore.html)有一堆方法和属性,只需将这些属性添加到'MemoryReactor'(和然后将IReactorCore添加到它的'@ implements'行)。接口只是对特定对象必须具有的特征的文档化描述。 – Glyph
gotcha ....他们是否真的需要做任何事情,或者我可以只为他们使用'pass'? – reubano