如果无法上传,请重新启动线程

时间:2014-05-03 08:43:06

标签: python multithreading threadpool

我正在使用threadpool使用S3上传。但是,我怀疑有时一个线程可能会失败。因此,我想重新启动失败的线程。我想知道如何实现这个目标:

pool = ThreadPool(processes=10)
pool.map(uploadS3_helper, args)

def uploadS3_helper(args):
    return uploadS3(*args)


def uploadS3(myfile, bucket_name, key_root, path_root, use_rel, define_region):
    if define_region:
        conn = S3Connection(aws_access_key_id=S3_ACCESS_KEY, aws_secret_access_key=S3_SECRET_KEY,
                        host='s3-us-west-2.amazonaws.com')
    else:
        conn = S3Connection(aws_access_key_id=S3_ACCESS_KEY, aws_secret_access_key=S3_SECRET_KEY)
    bucket = conn.get_bucket(bucket_name)
    print key_root + myfile
    print path_root
    print os.path.join(path_root, myfile)
    if use_rel:
        bucket.new_key(key_root + myfile).set_contents_from_file(open(os.path.join(path_root, myfile[1:])))
    else:
        bucket.new_key(key_root + myfile).set_contents_from_file(open(path_root))

1 个答案:

答案 0 :(得分:0)

要扩展@Martin James评论:考虑一个" retry decorator"。将链接代码添加到项目中,标记uploadS3功能,并尝试多次上传,每次失败后等待更长时间。