import os
import urllib
from datetime import datetime
import workerpool

class DownloadJob(workerpool.Job):    
    def __init__(self, fa):
        self.fa = fa 
    def run(self):        
        f = open(self.fa + '.txt','w')
        f.write('Example Note.......')
        f.close()  


pool = workerpool.WorkerPool(size=5)

def workfile():        
        range1 = 51
        range2 = 102
        fam1 = 555
        fam2 = 833
        
        ranges = range2 -range1
        fams = fam2 -fam1
        workname = "Python"
        
        
        path = os.getcwd()
                
        os.system('mkdir ' + str(workname))
        sTime = datetime.now()
        
        for a in range(ranges + 1):
                os.chdir(path + '\\' + str(workname))
                os.system('mkdir ' + str(range1 + a))
                os.chdir(path + '\\' + str(workname) + '\\' + str(range1 + a))
                
                for b in range(fams + 1):
                        fa = str(fam1 + b)
                        job = DownloadJob(fa)
                        pool.put(job)
                        pool.shutdown()
                        pool.wait()                                              
                        
                print 'Elapsed Time: %s' % (datetime.now() - sTime)
                z = open('info.txt','w')
                z.write('Elapsed Time: %s' % (datetime.now() - sTime))
                z.close()
                
        os.chdir(path + '\\' + str(workname))
        tumSure = open('info.txt','w')
        tumSure.write('Elapsed All Time: %s' % (datetime.now() - sTime))
        tumSure.close()
        print 'All Time: %s' % (datetime.now() - sTime)        
        print 'Workname : %s downloaded.' % (workname)
        quit()

workfile()

hi all ,
i have a code as above and i want to use thread logic for creating file but range1 folder start 51 it creates txt file name with 555.txt to 833.txt ... and than it creates folder with name 52 but stops and not creates 555.txt to 833.txt file....

it think it stops because

pool.shutdown()
                        pool.wait()

that code ..if i delete this line it creates folder but fams....

pls help...
i have to give my work on time....

I get error from import workerpool. Looks like not standard library.

Installed it with pip, and changed little code to see what happens, looks like it does a lot at least, when moving shutdown after collecting workers.

import os
import urllib
from datetime import datetime
import workerpool

class DownloadJob(workerpool.Job):    
    def __init__(self, fa):
        self.fa = fa+'.txt'
    def run(self):        
        f = open(self.fa,'w')
        f.write('Example Note.......')
        f.close()  
        print('Wrote %s' % self.fa)


pool = workerpool.WorkerPool(size=5)

def workfile():        
        range1 = 51
        range2 = 102
        fam1 = 555
        fam2 = 833
        
        ranges = range2 -range1
        fams = fam2 -fam1
        workname = "Python"
        
        
        path = os.getcwd()
                
        os.system('mkdir ' + str(workname))
        sTime = datetime.now()
        
        for a in range(ranges + 1):
                os.chdir(path + '\\' + str(workname))
                os.system('mkdir ' + str(range1 + a))
                os.chdir(path + '\\' + str(workname) + '\\' + str(range1 + a))
                
                for b in range(fams + 1):
                        fa = str(fam1 + b)
                        job = DownloadJob(fa)
                        pool.put(job)
                        pool.wait()                                              

                pool.shutdown()
                        
                print 'Elapsed Time: %s' % (datetime.now() - sTime)
                z = open('info.txt','w')
                z.write('Elapsed Time: %s' % (datetime.now() - sTime))
                z.close()
                
        os.chdir(path + '\\' + str(workname))
        tumSure = open('info.txt','w')
        tumSure.write('Elapsed All Time: %s' % (datetime.now() - sTime))
        tumSure.close()
        print 'All Time: %s' % (datetime.now() - sTime)        
        print 'Workname : %s downloaded.' % (workname)
        quit()

workfile()

Edited 5 Years Ago by pyTony: n/a

woow thnk you. but i want to ask u after loop finished will thread shutdown ?

This article has been dead for over six months. Start a new discussion instead.