Python Threading and sys.exit() codes

dickenson at serviceware.com dickenson at serviceware.com
Fri Mar 31 21:26:41 CEST 2000


I'm working my first multithreaded Python program. This program
compares the contents of an ftp site with a local copy. (It is not a
pure mirror site, but there is a 1-1 correspondence of files.) It then
ftp's only files which are newer on the source site than the target
site. There are often many files, so the ftp is set up to use multiple
threads, each of which repeatedly locks a global semaphore, pops a task
off a stack, releases the semaphore, and executes the task (a file to
ftp) ad nauseam.
My problem is this: I want to run this program as a scheduled task
under a job scheduler (currently testing cronDsys from #ifdef). The
single-threaded version of the program posts an appropriate code to the
environment via sys.exit(). The job scheduler traps this and takes
conditional action. So far, so good.
In the multi-threaded version of the program, the exit codes are not
getting posted and the main process is not terminating cleanly, at
least from the perspective of the job scheduler -- it appears to run
until manually terminated from the scheduler control console although
the NT command window disappears from the target desktop as expected.
The code fragments below are the ftp thread class and the __main__
section.
Can anybody help me with this ??
class FTPThread(Threading.Thread):
# def __init__(self,group,target,name,args,kwargs):
# print "in Init"
def run(self):
print self._Thread__name
print self._Thread__args
try:
ftp=ftplib.FTP('ftp.microsoft.com')
ftp.login('anonymous','dickenson at serviceware.com')
ftp.cwd('misc/kb')
except:
sys.exit(3)
subdir=self._Thread__args[0]
# print self,self._Thread__args
b=stacklen(self._Thread__args[1])
print "b at start=",b
while (b > 0):
# print "thread ",self._Thread__name," attempting to get
semaphore"
a=stacksem.acquire()
# print "thread ",self._Thread__name," got semaphore"
msfilepath=getstack(self._Thread__args[1])
newtime=getstack(self._Thread__args[2])
# print "thread ",self._Thread__name," attempting to release
semaphore"
a=stacksem.release()
# print "thread ",self._Thread__name," released semaphore"
#print "msfilepath",msfilepath
local0=string.replace(msfilepath,".","",1)
local1=string.replace(local0,"\\","")
localdrive=Target_Root+"updates\\\\"+subdir+"\\\\"
local=[localdrive,local1]
local2=string.join(local)
localfilepath=string.replace(local2," ","")
file=open(localfilepath,"w")
cmdstrings=["retr ",msfilepath]
cmd=string.join(cmdstrings)
try:
ftp.retrbinary(cmd,file.write)
print b," - file", msfilepath, "retrieved successfully
by thread ",self._Thread__name
if subdir=="revised":
revisecount=revisecount+1
if subdir=="new":
newcount=newcount+1
#print "success by thread ",self._Thread__name
#filecount=filecount+1
#print "filecount=",filecount
except NameError:
pass
else:
print sys.exc_info()
print "file ", msfilepath, " was not retrieved"
#print "failure by
thread ",self._Thread__name
file.close()
os.system("erase "+localfilepath)
sys.exit(1)
file.close()
# print "newtime",newtime
os.utime(localfilepath,(newtime,newtime))
b=stacklen(self._Thread__args[1])
# print "b at exit=",b
ftp.quit
sys.exit(0)
def notify(target,newcount,revisecount):
host="SNERT"
fromstring="Python Auto-FTP"
# tostring=["dickenson at serviceware.com"]
tostring=
["dickenson at serviceware.com","hlewis at serviceware.com","gkindel at servicewa
re.com","mciaramitaro at serviceware.com"]
msg="Subject: AutoFTP results\n\nUpdated files: " + str(revisecount)
+"\nNew files: "+str(newcount)+"\nlocated in subdirs off "+target
+ "\\\\updates"
s=smtplib.SMTP(host)
s.sendmail(fromstring,tostring,msg)
if __name__=='__main__':
Threadpool=12
stacksem=Threading.Semaphore(1)
Month_Num_Dict=
{'Jan':1,'Feb':2,'Mar':3,'Apr':4,'May':5, 'Jun':6,'Jul':7,'Aug':8,'Sep':
9,'Oct':10,'Nov':11,'Dec':12}
Target_Dict={}
Source_Dict={}
Fix_These=Stack(500)
Fix_Time=Stack(500)
New_These=Stack(500)
New_Time=Stack(500)
newcount=0
revisecount=0
Target_Root="S:\\\\Microsoft\\\\"
#Target_Root="M:\\\\msftp\\\\"
print "read source"
a=DateTime.now()
print a.hour, a.minute, a.second
read_source()
print "read target"
a=DateTime.now()
print a.hour, a.minute, a.second
read_target()
print "lookup source"
a=DateTime.now()
print a.hour, a.minute, a.second
newcount,revisecount=lookup_source()
print "start ftp..."
a=DateTime.now()
print a.hour, a.minute,a.second
threads=[]
for i in range(Threadpool):
my_thread=FTPThread(group=None,target=None,name=None,args=
("revised","Fix_These","Fix_Time"),kwargs={})
#my_thread.__init__(group=None,target=None,name="Revised
1",args=("revised","Fix_These","Fix_Time"),kwargs={})
my_thread.start()
threads.append(my_thread)
# for i in range(6):
my_thread=FTPThread(group=None,target=None,name=None,args=
("new","New_These","New_Time"),kwargs={})
# my_thread.__init__(group=None,target=None,name="New 1",args=
("new","New_These","New_Time"),kwargs={})
my_thread.start()
threads.append(my_thread)
for thread in threads:
thread.join()
print "all threads finished"
try:
notify(Target_Root,newcount,revisecount)
print "notify done"
finally:
sys.exit(0)


Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list