【发布时间】:2015-02-13 05:25:05
【问题描述】:
最近,我需要实现一个程序,以尽快将驻留在 Amazon EC2 中的文件上传到 Python 中的 S3。文件大小为30KB。
我尝试了一些解决方案,使用多线程、多处理、协程。以下是我在 Amazon EC2 上的性能测试结果。
3600(文件数量)* 30K(文件大小)~~ 105M(总计)--->
**5.5s [ 4 process + 100 coroutine ]**
10s [ 200 coroutine ]
14s [ 10 threads ]
代码如下所示
用于多线程
def mput(i, client, files):
for f in files:
if hash(f) % NTHREAD == i:
put(client, os.path.join(DATA_DIR, f))
def test_multithreading():
client = connect_to_s3_sevice()
files = os.listdir(DATA_DIR)
ths = [threading.Thread(target=mput, args=(i, client, files)) for i in range(NTHREAD)]
for th in ths:
th.daemon = True
th.start()
for th in ths:
th.join()
对于协程
client = connect_to_s3_sevice()
pool = eventlet.GreenPool(int(sys.argv[2]))
xput = functools.partial(put, client)
files = os.listdir(DATA_DIR)
for f in files:
pool.spawn_n(xput, os.path.join(DATA_DIR, f))
pool.waitall()
用于多处理 + 协程
def pproc(i):
client = connect_to_s3_sevice()
files = os.listdir(DATA_DIR)
pool = eventlet.GreenPool(100)
xput = functools.partial(put, client)
for f in files:
if hash(f) % NPROCESS == i:
pool.spawn_n(xput, os.path.join(DATA_DIR, f))
pool.waitall()
def test_multiproc():
procs = [multiprocessing.Process(target=pproc, args=(i, )) for i in range(NPROCESS)]
for p in procs:
p.daemon = True
p.start()
for p in procs:
p.join()
本机配置为Ubuntu 14.04,2个CPU(2.50GHz),4G内存
达到的最高速度约为 19Mb/s (105 / 5.5)。总的来说,速度太慢了。有什么办法可以加快速度? stackless python 能不能做得更快?
【问题讨论】:
-
有趣的是,如果我将文件大小设置为 1M,我可以获得 > 90Mb/s。
-
问题比答案提供更多信息 =D 感谢协程示例
标签: python amazon-web-services amazon-s3 coroutine