我运行了几个 python 子进程来将数据迁移到 S3。我注意到我的 python 子进程经常下降到 0% 并且这种情况持续超过一分钟。这会显着降低迁移过程的性能。
子进程做这些事情:
为每个表生成子进程。
for table in tables:
print "Spawn process to process {0} table".format(table)
process = multiprocessing.Process(name="Process " + table,
target=target_def,
args=(args, table))
process.daemon = True
process.start()
processes.append(process)
for process in processes:
process.join()
使用限制和偏移从数据库查询数据。我使用 PyMySQL 库来查询数据。
将返回的数据转换为另一种结构。construct_structure_def()
是将行转换为另一种格式的函数。
buffer_string = []
for i, row_file in enumerate(row_files):
if i == num_of_rows:
buffer_string.append( json.dumps(construct_structure_def(row_file)) )
else:
buffer_string.append( json.dumps(construct_structure_def(row_file)) + "\n" )
content = ''.join(buffer_string)
将转换后的数据写入文件并使用 gzip 进行压缩。
with gzip.open(file_path, 'wb') as outfile:
outfile.write(content)
return file_name
将文件上传到 S3。
为了加快速度,我使用multiprocesses.Process
内置库为每个表创建子进程。
我在虚拟机中运行了我的脚本。以下是规格:
我在这里的帖子中看到,主要的可能性之一是由于内存 I/O 限制。因此,我尝试运行一个子流程来测试该理论,但无济于事。
知道为什么会这样吗?如果你们需要更多信息,请告诉我。
先感谢您!
原来罪魁祸首是我运行的查询。查询花了很长时间才返回结果。这使得 python 脚本空闲,因此使用率为零。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句