Some code is below. It is used to take a bunch (1000s) of files and merge them into 
one big file. I hope it helps. The hardest part of writing it was debugging the worker 
thread. If everythong doesn't go just right, then you end up with a bunch of threads 
out there in memory taking up cycles.

I do have one (related) problem with doing this. In another (similar) application, I 
tried to use this technique to prevent server timeouts. The problem was that my worker 
thread was trying to eval a huge (~40MB) dictionary. This eval seemed to block 
everything in the entire Webware process. Now if I read the Python documentation 
correctly, that happens because Python's threads are simulated in the interpreter and 
each operation is viewed as atomic. The eval was taking a long time and would not 
allow any other thread in the process to do it's thing. Is this correct? If not, what 
is happening? If so, what other way can I do this?

--John

-- file b_admin_movefiles.py --

import threading

class Worker(threading.Thread):
    def __init__(self, *args, **kwds):
        threading.Thread.__init__(self, *args, **kwds)

    def run(self):
        pass

from b_top_admin import b_top_admin
import file_mod
import useful_mod
from TravelSync_LocalOut import TravelSync_LocalOut
import random

class b_admin_movefiles(b_top_admin):

    def doWork(self):
        ses = self.session()
        comp_loc = ses.value('comp_loc')

        packer = TravelSync_LocalOut()
        fran = file_mod.getCompanyDir(comp_loc)
        date = file_mod.getCompanyDate(comp_loc)
        location = ses.value('location')
        result = 'ok'

        if location:
            result = 'done'
            result = packer.Pack(fran,date,location) # this line is the time-intensive 
one
        else:
            result = 'bad location'
        ses.setValue('doWorkResult',result)

    def actions(self):
        return ['move']

    def move(self):
        ses = self.session()
        fields = self.request().fields()
        if not ses.hasValue('location'):
            location = fields.get('location')
            ses.setValue('location', location)
        if not ses.hasValue('workerCreated'):
            ses.setValue('workerCreated',1)
            self.worker = Worker()
            self.worker.run = self.doWork
        if not ses.hasValue('workerStarted'):
            self.worker.start()
            ses.setValue('workerStarted',1)
        if ses.hasValue('doWorkResult'):
            self.worker.join()
            del self.worker
            ses.delValue('workerStarted')
            ses.delValue('workerCreated')
            result = ses.value('doWorkResult')
            ses.delValue('doWorkResult')
            ses.delValue('location')
            self.writeBody(result)
        else:
            
self.response().setHeader('Refresh','3;URL=b_admin_movefiles.py?_action_=move&rand=%s'%random.randrange(1000))
            self.writeBody('Moving all files. Please do not close your browser window 
or go to any other page.')

    def writeContent(self,msg=''):
        ses = self.session()
        comp = ses.value('comp_name','Company')
        comp_loc = ses.value('comp_loc')

        if msg:
            self.writeln(msg + '<br />')

        if not ses.hasValue('workerStarted'):
            self.writeln('Please enter the location to which you want to save the 
data.<br />')
            
            self.writeln("<form method='POST'>")
            self.writeln("<input type=text size=60 name=location>")
            self.writeln("<input type=submit name=_action_move value='MOVE'>")
            
            self.writeln("</form>")

-- end file --


-------------------------------------------------------
This SF.Net email is sponsored by: Oracle 10g
Get certified on the hottest thing ever to hit the market... Oracle 10g. 
Take an Oracle 10g class now, and we'll give you the exam FREE.
http://ads.osdn.com/?ad_id=3149&alloc_id=8166&op=click
_______________________________________________
Webware-discuss mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/webware-discuss

Reply via email to