多线程与workerpool模块

时间:2011-08-26 09:02:41

标签: python multithreading threadpool pool

import os
import urllib
import workerpool

from datetime import datetime

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()

大家好,

我有一个上面的代码,我想使用线程逻辑来创建文件。文件夹编号从range1开始,即51.文本文件在此目录中创建,名称为555.txt833.txt。但是在创建名称为52的文件夹后,它会停止,无法创建555.txt833.txt

它认为它会停止,因为

pool.shutdown()
pool.wait()

如何让循环继续不停?

1 个答案:

答案 0 :(得分:3)

我认为你应该只在你完成它时关闭池,即在for a循环之后,甚至在try...finally子句中。

看起来会这样:

try:
    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)
finally:
    pool.shutdown()
    pool.wait()

以这种方式pool关闭

a)只有在完成所有池放置时才会发生 b)即使存在例外以便彻底关闭也会发生。

如果池有一个上下文管理器,那就更简单了。但是AFAICS,it hasn't。 否则,你可以做

with pool:
    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)

但如果你愿意,你可以做到

from contextlib import contextmanager

@contextmanager
def shutdown_wait(pool):
    try:
        yield pool
    finally:
        pool.shutdown()
        pool.wait()

...
with shutdown_wait(pool):
    for a ... [as above]