Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add timeout to Deferred from Twisted's deferToThread API?

Tags:

python

twisted

from twisted.internet import reactor
from twisted.internet import threads
from twisted.internet import defer
import time

def worker(arg):
    print 'Hello world'
     time.sleep(10)
    return 1

def run():
    print 'Starting workers'
    l = []
    for x in range(2):
        l.append(threads.deferToThread(worker, x))
    return defer.DeferredList(l)

def res(results):
    print results
    reactor.stop()

d = run()
d.addCallback(res)
reactor.run()

How to stop workers by timeout ?

like image 986
Bdfy Avatar asked Apr 18 '11 09:04

Bdfy


1 Answers

Threads cannot be interrupted unless they cooperate with you. time.sleep(10) is not going to cooperate, so I don't think you can interrupt this worker. If you have another kind of worker that has several discrete phases, or operates in a loop over some tasks, then you can do something like this:

def worker(stop, jobs):
    for j in jobs:
        if stop:
            break
        j.do()

stop = []
d = deferToThread(worker)

# This will make the list eval to true and break out of the loop.
stop.append(None)

This isn't Twisted specific, either. This is just how threads work in Python.

like image 107
Jean-Paul Calderone Avatar answered Sep 28 '22 04:09

Jean-Paul Calderone