Problem: 'Threads' in Python?

Ralph Sluiters ralph at sluiters.de
Tue Jan 6 11:07:45 CET 2004


> Wouldn't a better approach be to decouple the cache mechanism from the cgi
> script?  Have a long-running Python process act as a memoizing cache and
> delegate requests to the slow function.  The cgi scripts then connect to
> this cache process (via your favorite IPC mechanism).  If the cache
process
> has a record of the call/request, it returns the previous value
immediately,
> and updates its cache in the meantime.  If it doesn't have a record, then
it
> blocks the cgi script until it gets a result.
The caching can not be decoupled, because the cgi-script gets an folder ID
gets only data from this "folder". So if I decouple die processes, I don't
know which folders to cache and I can not cache all folders, because the
routine is to slow. So I must get the actual folder from cgi and then cache
this one as long as the uses is in this folder and pulls data every 2
Minutes and cache another folder, if
the uses changes his folder.

> How can threading help you if the cgi-process dies after each request
unless
> you store the value somewhere else?  And if you store the value somewhere,
> why not have another process manage that storage?  If it's possible to
> output a complete page before the cgi script terminates (I don't know if
the
> server blocks until the script terminates), then you could do the cache
> updating afterwards.  In this case I guess you could use a pickled
> dictionary or something as your cache, and you don't need a separate
> process.  But even here you wouldn't necessarily use threads.
The data is to large to store it in the memmory and with this method, as you
said, threading wouldn't help, but I store the data in the disk.

My code:

#Read from file
try:
    oldfile = open(filename,"r")
    oldresult =string.joinfields(oldfile.readlines(),'\r\n')
    oldfile.close()
except:
    # Start routine
    oldresult = get_data(ID) # Get xml data
# Print header, so that it is returned via HTTP
print string.joinfields(header, '\r\n')
print oldresult

# ***

# Start routine
result = get_data(ID) # Get xml data
#Save to file
newfile = open(filename, "w")
newfile.writelines(result)
newfile.close()
#END

At the position *** the rest of the script must be uncoupled, so that the
client can proceed with the actual data, but the new data generation for the
next time ist stored in a file.

Ralph





More information about the Python-list mailing list