Search code examples
parameterstwistedparallel-processingdeferred

Using Twisted Deferreds with parallel callbacks, when each is given a different argument


I want to create a deferred as follows:

f1(x1) and f2(x2) are performed in parallel (so to speak) and after they finish, I run f3() If I had the same parameters, I'd run:

d = Deferred()
d.addCallbacks(f1)
d.addCallbacks(f2)
d.addCallback(lambda x: f3())
d.callback(x1)

So that I pass x1 to both f1 and f2. But I need f1 to get x1 and so forth.

How can I do this?

Thanks.


Solution

  • I am not sure, if I understood your use case right, but this seems to be something, where a DeferredList would work particularly well.

    d1 = function_that_returns_a_deferred_1(x1)
    d2 = function_that_returns_a_deferred_2(x2)
    d = DeferredList([d1, d2])
    d.addCallback(lambda ign: f3())
    

    This way, f3 will only be executed once both d1 and d2 have completed.