[Twisted-Python] question about threading

Phil Christensen phil at bubblehouse.org
Tue Oct 25 11:05:21 EDT 2005

hey folks,

i've got a quick question for anyone that can help me out. i'm also  
in the process of looking this up in the twisted docs/code, but this  
is some last minute coding for a big demo tomorrow, so i'm hedging my  
bets by posting here.

i've got a relatively simple async twisted server that has a custom  
protocol. most everything the server needs to do on a receipt of a  
message is short running, but i have one task that isn't.

i need to spawn a thread that executes a couple of binaries (html2ps  
and ps2pdf) which are generally long-running. after executing those  
binaries, i need to send a message back to the client that sent the  
original command to generate the pdf. i tried just creating a basic  
thread that holds onto a reference to the client object, but it  
doesn't seem to send the command back to the client until i send  
another message to the server (any kind of message).

i know there's a bunch of reactor methods that deal with threads, but  
i've never used them before, and i'm not sure which one will fix this  

here's my thread subclass:

class PDFGenerator(threading.Thread):
     def __init__(self, client):
         self.client = client

     def run(self):
         pres = event.getActivePresentation()
         t = datetime.datetime.now()
         base_name = 'sample-file-name'
         out_html = file('/tmp/' + base_name + '.html', 'w')
         # [snip snip snip]
         # write some html to the file
         os.system('html2ps /tmp/' + base_name + '.html > /tmp/' +  
base_name + '.ps')  # > /tmp/' + base_name + '_ps.log')
         os.system('mkdir data/transcripts/' + str(pres.id))
         os.system('ps2pdf /tmp/' + base_name + '.ps data/ 
transcripts/' + str(pres.id) + '/' + base_name + '.pdf > /tmp/' +  
base_name + '_pdf.log')
         lock = threading.Lock()
         self.client.sendCommand('presentTranscript', ['/ 
transcripts/' + str(pres.id) + '/' + base_name + '.pdf'])

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")

i made sure to set the lock, but i think the problem is in how i'm  
using threads. the clue i'm seeing is that in the log, i see the  

2005/10/25 10:40 EDT [SSProtocol,25,] Got generateTranscript 
('') from IPv4Address(TCP, '', 49179)
2005/10/25 10:40 EDT [-] Sending presentTranscript,/transcripts/ 
phil_bubblehouse.org_2005-10-25-10-40-13.pdf to IPv4Address(TCP,  
'', 49179)

you can see that the second log entry doesn't print the protocol  
object that's generating the message.

i know there's something simple i'm not doing; any help would be  


More information about the Twisted-Python mailing list