Threading help?

Jason Orendorff jason at jorendorff.com
Sat Mar 9 21:10:49 EST 2002


VanL wrote:
> I can do each of the individual parts, but I don't know how to combine
> them all so that each is running in a different thread, so that process
> 2 doesn't make the other two lose their timing.

# NOTE:  The code below ignores synchronization issues.  This is actually
# okay for this situation, I think, because assigning or accessing the
# value of an attribute is guaranteed atomic.  But in general you would
# want to protect all access to data that's common across threads,
# using e.g. threading.RLock.
#
# Your requirements ask for the data processing to occur every 0.1 sec.
# This doesn't exactly do that, but you can delete the time delay if
# this really needs to run *constantly*.
#
# It would be more typical to fetch the data on demand, then cache
# it in case another response comes in at about the same time.
# Then you wouldn't need threads at all.

import threading, time, urllib, BaseHTTPServer

# First, start a processing thread.

class ProcessingThread(threading.Thread):
    """ Thread for fetching and formatting data.

    Attribute:
    self.result - a tuple with (timestamp, data).
    """

    def __init__(self):
        self.result = (0, '')
        self.done = 0

    def run(self):
        while not self.done:
            data = self.fetch_new_data()
            output = self.process(data)
            self.result = (time.time(), output)
            time.sleep(0.1)

    def fetch_new_data(self):
        ... use urllib to get new data ...

    def process(self, data):
        ... do something with the data ...
        ... format it for output ...
        return result

processing_thread = ProcessingThread()
processing_thread.start()

# In your main thread, do your server stuff.
# This is of course example code.

class MyHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
    def do_GET(self):
        timestamp, output = processing_thread.result
        self.wfile.write("HTTP/1.0 200 OK\r\n"
                         ...
                         ... your headers here ...
                         ...
                         "Date: " + format(timestamp) + "\r\n"
                         "Connection: close\r\n"
                         "Content-type: text/html;charset=US-ASCII\r\n"
                         "Content-length: " + str(len(output)) + "\r\n"
                         "\r\n")
        self.wfile.write(result)

server = BaseHTTPServer.HTTPServer(('', 8000), MyHTTPRequestHandler)
server.serve_forever()


## Jason Orendorff    http://www.jorendorff.com/




More information about the Python-list mailing list