[Twisted-Python] question about threading

Itamar Shtull-Trauring itamar at itamarst.org
Tue Oct 25 11:32:16 EDT 2005

On Tue, 2005-10-25 at 11:05 -0400, Phil Christensen wrote:

>          lock = threading.Lock()
>          lock.acquire()
>          self.client.sendCommand('presentTranscript', ['/ 
> transcripts/' + str(pres.id) + '/' + base_name + '.pdf'])
>          lock.release()

> the client object holds a reference to the protocol object, and  
> sendCommand basically just executes:
>          print "Sending " + message + " to " + str(self.protocol.source)
>          self.protocol.transport.write(message + "\0")

Twisted is *not* thread-safe. You can't call its methods from another
thread like that.

Also, os.system() will probably not work on Unix in Twisted; instead,
you can use Twisted's non-blocking process support to run commands
(reactor.spawnProcess, or in your case
twisted.internet.utils.getProcessOutput()). Then you won't need to use
threads at all.

There is a way of doing what you want with threads (see
http://twistedmatrix.com/projects/core/documentation/howto/threading.html) but you should not be using threads in this case as you don't need to and it's just a source of bugs if you're not careful.

More information about the Twisted-Python mailing list