好的,我根据@Matt Billenstien 的提示想出了一个解决方案。它使用 eventlet 库。第一步在这里是最重要的(标准 IO 库的猴子补丁)。
使用 nohup 在后台运行此脚本,一切就绪。
from eventlet import *
patcher.monkey_patch(all=True)
import os, sys, time
from boto.s3.connection import S3Connection
from boto.s3.bucket import Bucket
import logging
logging.basicConfig(filename="s3_download.log", level=logging.INFO)
def download_file(key_name):
# Its imp to download the key from a new connection
conn = S3Connection("KEY", "SECRET")
bucket = Bucket(connection=conn, name="BUCKET")
key = bucket.get_key(key_name)
try:
res = key.get_contents_to_filename(key.name)
except:
logging.info(key.name+":"+"FAILED")
if __name__ == "__main__":
conn = S3Connection("KEY", "SECRET")
bucket = Bucket(connection=conn, name="BUCKET")
logging.info("Fetching bucket list")
bucket_list = bucket.list(prefix="PREFIX")
logging.info("Creating a pool")
pool = GreenPool(size=20)
logging.info("Saving files in bucket...")
for key in bucket.list():
pool.spawn_n(download_file, key.key)
pool.waitall()