DeferToThread vs Deferred ()

I would like to understand what happens to the code snippets below -

SNIPPET # 1

from twisted.internet import threads, defer, reactor

def proc1(a):
    while True:
        print "Proc----------1"

def proc2(a):
    while True:
        print "Proc----------2"

def proc3(a):
    while True:
        print "Proc----------3"

d1 = threads.deferToThread(proc1)
d2 = threads.deferToThread(proc2)
d3 = threads.deferToThread(proc3)

reactor.run()

My understanding is that all threads are executed in parallel, and the output => stdout of all mixed processes.

Snipper # 2

from twisted.internet import threads, defer, reactor

def proc1(a):
    while True:
        print "Proc----------1"

def proc2(a):
    while True:
        print "Proc----------2"

def proc3(a):
    while True:
        print "Proc----------3"



d1 = defer.Deferred()
d2 = defer.Deferred()
d3 = defer.Deferred()

d1.addCallback(proc1)
d2.addCallback(proc2)
d3.addCallback(proc3)

d1.callback('a')
d2.callback('a')
d3.callback('a')

reactor.run()

And for this snippet - each deferred callback is launched one after another, and if it comes to exit, only proc1 stdouts will be idle.

Please correct me if I am wrong. Thus, basically, what I want to understand and confirm is that deferred objects are launched one after another, while deferToThread are launched in parallel, as in a stream of names.

+3
source share
2 answers

Please correct me if I am wrong. Thus, basically, what I want to understand and confirm is that deferred objects are launched one after another, while deferToThread are launched in parallel, as in a stream of names.

, . , :

d1.callback('a')
d2.callback('a')
d3.callback('a')

. . , Python.

. , . ( addCallback), ( callback).

+4

.

Snippet 1 ( ). .

2 . , .

, , - , , , , , .

, ( , )

+1

All Articles