[Twisted-Python] locking threads when deferToThread is used

Phil Mayers p.mayers at imperial.ac.uk
Wed Aug 8 17:21:33 EDT 2007

> class Getter:
>      def gotResults(self, domain, test_opts):
>        if len(domain) != 0:
>            self.d.callback(siptest_f(domain, test_opts))
>        else:
>            self.d.errback(ValueError("You used an odd number!"))

gotResults is inside a thread, and you're touching the deferred. Don't.
Just do this:

class MyWeirdTester:
  def gotResults(...):
    if domain:
      return siptest_f(domain, test_opts)
    raise ValueError("you used an odd number")

  def getServerResponse(...):
    d = deferToThread(self.gotResults, ...)
    d.addCallback(self._print, ...)
    return d

More information about the Twisted-Python mailing list