|
510.
|
|
|
If <code>callback</code> has been called on this Deferred already, call all the callbacks that have been added to this Deferred since <code>pause</code> was called.
|
|
|
type: Content of: <html><body><ul><li><p>
|
|
|
|
如果 Deferred 的 <code>callback</code> 函数已经被调用,则调用 <code>pause</code> 被调用之后的加入的回调函数。
|
|
Translated and reviewed by
Liu, Kun
|
|
|
|
Located in
howto/gendefer.xhtml:156
|
|
511.
|
|
|
Whether it was called or not, this will put this Deferred in a state where further calls to <code>addCallbacks</code> or <code>callback</code> will work as normal.
|
|
|
type: Content of: <html><body><ul><li><p>
|
|
|
|
无论 <code>pause</code> 是否被调用过, <code>unpause</code> 都会让这个 Deferred 之后的 <code>addCallbacks</code> 或 <code>callback</code> 调用回到正常状态。
|
|
Translated and reviewed by
Liu, Kun
|
|
|
|
Located in
howto/gendefer.xhtml:161
|
|
512.
|
|
|
Returning Deferreds from synchronous functions
|
|
|
type: Content of: <html><body><h2>
|
|
|
|
同步函数返回 Deferred
|
|
Translated and reviewed by
Liu, Kun
|
|
|
|
Located in
howto/gendefer.xhtml:168
|
|
513.
|
|
|
Sometimes you might wish to return a Deferred from a synchronous function. There are several reasons why, the major two are maintaining API compatibility with another version of your function which returns a Deferred, or allowing for the possiblity that in the future your function might need to be asynchronous.
|
|
|
type: Content of: <html><body><p>
|
|
|
|
有时,你可能想让一个同步函数返回一个 Deferred。这么做有很多理由,其中最主要的两个是:保持跟 API 中其他返回 Deferred 的函数相一致;为今后函数可能需要写成异步做准备。
|
|
Translated and reviewed by
Liu, Kun
|
|
|
|
Located in
howto/gendefer.xhtml:170
|
|
514.
|
|
|
In the <a href="defer.xhtml">Using Deferreds</a> reference, we gave the following example of a synchronous function:
|
|
|
type: Content of: <html><body><p>
|
|
|
|
在前面的 <a href="defer.xhtml">Deferred 使用指南</a>中,我们给过一个同步函数的例子,如下所示:
|
|
Translated and reviewed by
Fantix King
|
|
|
|
Located in
howto/gendefer.xhtml:176
|
|
515.
|
|
|
While we can require that callers of our function wrap our synchronous result in a Deferred using <code class="API" base="twisted.internet.defer">maybeDeferred</code>, for the sake of API compatibility it is better to return a Deferred ourself using <code class="API" base="twisted.internet">defer.succeed</code>:
|
|
|
type: Content of: <html><body><p>
|
|
|
|
虽然我们可以要求调用者将函数的同步结果用 <code class="API" base="twisted.internet.defer">maybeDeferred</code> 包装在一个 Deferred 里,但是为了 API 的兼容性,最好还是使用 <code class="API" base="twisted.internet">defer.succeed</code> 直接返回一个 Deferred。
|
|
Translated and reviewed by
Liu, Kun
|
|
|
|
Located in
howto/gendefer.xhtml:182
|
|
516.
|
|
|
from twisted.internet import defer
def immediateIsValidUser(user):
'''
Returns a Deferred resulting in true if user is a valid user, false
otherwise
'''
result = user in ["Alice", "Angus", "Agnes"]
# return a Deferred object already called back with the value of result
return defer.succeed(result)
|
|
|
type: Content of: <html><body><pre>
|
|
|
represents a line break.
Start a new line in the equivalent position in the translation.
|
|
|
represents a space character.
Enter a space in the equivalent position in the translation.
|
|
|
|
from twisted.internet import defer
def immediateIsValidUser(user):
'''
返回 Deferred 包含结果:真 如果 user 是有效用户,否则为 假
'''
result = user in ["Alice", "Angus", "Agnes"]
# 返回一个 Deferred 对象,已经用计算结果激活了正常回调函数处理链
return defer.succeed(result)
|
|
Translated and reviewed by
Liu, Kun
|
|
|
|
Located in
howto/gendefer.xhtml:189
|
|
517.
|
|
|
There is an equivalent <code class="API" base="twisted.internet">defer.fail</code> method to return a Deferred with the errback chain already fired.
|
|
|
type: Content of: <html><body><p>
|
|
|
|
这里有个等价的 <code class="API" base="twisted.internet">defer.fail</code> 方法,返回一个 Deferred,已激活错误回调函数处理链。
|
|
Translated and reviewed by
Liu, Kun
|
|
|
|
Located in
howto/gendefer.xhtml:203
|
|
518.
|
|
|
Integrating blocking code with Twisted
|
|
|
type: Content of: <html><body><h2>
|
|
|
|
用 Twisted 集成阻塞代码
|
|
Translated and reviewed by
Liu, Kun
|
|
|
|
Located in
howto/gendefer.xhtml:207
|
|
519.
|
|
|
At some point, you are likely to need to call a blocking function: many functions in third party libraries will have long running blocking functions. There is no way to 'force' a function to be asynchronous: it must be written that way specifically. When using Twisted, your own code should be asynchronous, but there is no way to make third party functions asynchronous other than rewriting them.
|
|
|
type: Content of: <html><body><p>
|
|
|
|
到一定的时候,你很可能需要调用阻塞的函数——很多第三方库中都有需要长时间运行的阻塞函数。没有办法去强行把函数变为异步——那必须用特定的方式写成。当使用 Twisted 时,你自己的代码应该是异步的,但没法让第三方的函数也是异步的,除非重写。
|
|
Translated and reviewed by
Liu, Kun
|
|
|
|
Located in
howto/gendefer.xhtml:209
|