我正在运行一个脚本,将数百万(确切地说是240万)图像从几个gcs桶复制到一个中央桶中,所有桶都在同一个区域。我最初是从一个csv文件开始工作的,但将其分解为64个较小的文件,这样每个进程都可以迭代自己的文件,而不必等待其他文件。当脚本在GCP上的64 vCPU,240 GB内存实例上启动时,它可以正常运行大约一个半小时。在75分钟内复制了155 000个文件。CPU使用率为持续99%。在此之后,CPU使用率急剧下降到2%,传输速率显著下降。我真的不知道为什么。我将通过在错误目录中创建空白文件来跟踪失败的文件。这样,在写入中央错误文件时不存在写锁。密码在下面。这不是一个间距或语法错误,当我复制到文章中时,一些空格被搞乱了。任何帮助都是非常感谢的。
谢了,扎克
import os
import subprocess
import csv
from multiprocessing.dummy import Pool as ThreadPool
from multiprocessing import Pool as ProcessPool
import multiprocessing
gcs_destination = 'gs://dest-bucket/'
source_1 = 'gs://source-1/'
source_2 = 'gs://source-2/'
source_3 = 'gs://source-3/'
source_4 = 'gs://source-4/'
def copy(img):
try:
imgID = img[0] # extract name
imgLocation = pano[9] # extract its location on gcs
print pano[0] + " " + panoLocation
source = ""
if imgLocation == '1':
source = source_1
elif imgLocation == '2':
source = source-2
elif imgLocation == '3':
source = source_3
elif imgLocation == '4':
source = source_4
print str(os.getpid())
command = "gsutil -o GSUtil:state_dir=.{} cp {}{}.tar.gz {}".format(os.getpid(), source, imgID , g
prog = subprocess.call(command, shell="True")
if prog != 0:
command = "touch errors/{}_{}".format(imgID, imgLocation)
os.system(command)
except:
print "Doing nothing with the error"
def split_into_threads(csv_file):
with open(csv_file) as f:
csv_f = csv.reader(f)
pool = ThreadPool(15)
pool.map(copy, csv_f)
if __name__ == "__main__":
file_names = [None] * 64
# Read in CSV file of all records
for i in range(0,64):
file_names[i] = 'split_origin/origin_{}.csv'.format(i)
process_pool = ProcessPool(multiprocessing.cpu_count())
process_pool.map(split_into_threads, file_names)发布于 2018-12-15 18:50:28
对于gsutil,我强烈同意通过添加-m来进行多线程处理的建议。此外,复合上传,-o,可能是不必要和不受欢迎的,因为图像不是每个大小的GB,不需要分割成碎片。很可能在XXMB的范围内。
在python函数中,您正在调用gsutil命令,这些命令反过来调用进一步的python函数。使用下面提供的google制作的python客户端库应该更干净,性能更好。Gsutil是为交互式CLI使用构建的,而不是用于编程调用的。
https://cloud.google.com/storage/docs/reference/libraries#client-libraries-install-python
另外,对于gsutil,请查看您的~/..boto文件,并查看多处理和多线程值。加速机可以处理更大的线程和进程。作为参考,我从我的Macbook Pro w/ 1进程和24个线程工作。我使用以太网适配器和硬线连接到我的办公室连接,并获得难以置信的性能在内部SSD (>450 Mbps)。那是兆位,不是字节。尽管如此,转移率还是令人印象深刻。
发布于 2018-03-11 10:30:34
https://stackoverflow.com/questions/49162182
复制相似问题