Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass extra arguments to callback register functions with twisted python api?

Tags:

python

twisted

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)

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.

like image 331
curious Avatar asked Oct 25 '11 14:10

curious


People also ask

How do you pass a callback function as a parameter in Python?

First define two functions, the callback and the calling code, then pass the callback function into the calling code. The callback function has access to the variables and parameters of the calling function.,In these primitive examples, the use of a callback is primarily a demonstration of principle.

How do you pass many arguments in python?

We can pass multiple arguments to a python function by predetermining the formal parameters in the function definition.

What are the two methods of passing arguments to functions in Python?

5 Types of Arguments in Python Function Definition:keyword arguments. positional arguments. arbitrary positional arguments. arbitrary keyword arguments.


1 Answers

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)

All arguments passed like this to addCallback (or addErrback) are passed on to the callback function.

like image 113
Jean-Paul Calderone Avatar answered Nov 01 '22 04:11

Jean-Paul Calderone