如何使用扭曲的python api将额外的参数传递给回调寄存器函数? [英] How to pass extra arguments to callback register functions with twisted python api?
问题描述
我有以下使用扭曲 API 的 python 代码.
I have the following python code using the twisted API.
def function(self,filename):
def results(result):
//do something
for i in range(int(numbers)) :
name = something that has to do with the value of i
df = function_which_returns_a defer(name)
df.addCallback(results)
它使用 Twisted API.我想要实现的是将在每次迭代中构造的名称的值传递给回调函数(结果),而无需更改 functions_which_returns_a defer() 函数的内容,当然还有延迟对象.在functions_which_returns_a defer 的每个结果中,名称的值都应该传递给results() 以对此进行处理.即:在第一次迭代时,当执行到达结果函数时,我需要该函数在 i=0 时将延迟对象的结果与 name 的值一起保存,然后当 i=1 时,延迟对象将与值一起传递名称,等等.因此,每次使用名称变量和名称变量调用时,我都需要延迟对象的结果.当我尝试直接使用 nameinside results() 的值时,它始终保持最后一次迭代的值,这是基本原理,因为 function_which_returns_a defer(name) 尚未返回.
It uses the Twisted API. What i want to achieve is to pass to the callbacked function (results) the value of the name which is constructed in every iteration without changing the content of the functions_which_returns_a defer() function along with the deferred object of course. In every result of the functions_which_returns_a deffer the value of the name should be passed to results() to do something with this. I.e: at the first iteration when the execution reach the results function i need the function hold the result of the deffered object along with the value of name when i=0,then when i=1 the defered object will be passed with the value of name, and so on.So i need every time the result of the defer object when called with the name variable alond with the name variable. When i try to directly use the value of nameinside results() it holds always the value of the last iteration which is rationale, since function_which_returns_a defer(name) has not returned.
推荐答案
您可以通过简单地传递额外参数到 Deferred.addCallback
调用站点的 Deferred
回调Deferred.addCallback
的参数:
You can pass extra arguments to a Deferred
callback at the Deferred.addCallback
call site by simply passing those arguments to Deferred.addCallback
:
def function(self,filename):
def results(result, name):
# do something
for i in range(int(numbers)) :
name = something that has to do with the value of i
df = function_which_returns_a defer(name)
df.addCallback(results, name)
您也可以通过关键字传递参数:
You can also pass arguments by keyword:
df.addCallback(results, name=name)
像这样传递给addCallback
(或addErrback
)的所有参数都会传递给回调函数.
All arguments passed like this to addCallback
(or addErrback
) are passed on to the callback function.
这篇关于如何使用扭曲的python api将额外的参数传递给回调寄存器函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!