Python twisted thread

Hi, please tell me how to use different functions in different threads using the thread pool in twisted ... say

I have a list of identifiers x=[1,2,3,4], where 1,2, ... etc. are identifiers (I got from the database, and each of them contains a python script in some place on the disk).

what i want to do is

scanning x traverses in the list and running each script in different threads until they end


Thanx Calderone, your code helped me a lot.

I have little doubt that I can resize the threadpool this way.

from twisted.internet import reactor
reactor.suggestThreadPoolSize(30)

They say that all 30 available threads are busy, and there are still identifiers in the list (dict or tuple) 1 - In this situation, will all identifiers pass? I mean, as soon as the thread becomes free, the next tool (id) will be assigned to the freed thread? 2 - there are also some cases when one tool must be executed before the second tool, and one output of the tool will be used by another tool, as it will be controlled in a twisted thread. 3

+3
source share
1 answer

Twisted twisted.internet.threads.deferToThread. , , , twisted.internet.threads.deferToThreadPool. , . , . Deferred , , , .

from twisted.internet.threads import deferToThread
from twisted.internet.defer import gatherResults
from twisted.internet import reactor

def double(n):
    return n * 2

data = [1, 2, 3, 4]

results = []
for datum in data:
    results.append(deferToThread(double, datum))

d = gatherResults(results)
def displayResults(results):
    print 'Doubled data:', results
d.addCallback(displayResults)
d.addCallback(lambda ignored: reactor.stop())

reactor.run()

Twisted Threading howto.

+14

Source: https://habr.com/ru/post/1754540/


All Articles