python 文件不完整,如何等到它完成后再处理回父进程
python file is incomplete, how to wait till it is complete before handling back to parent process
我希望能够启动一个子进程,该子进程将文件写入服务器,然后等到它完全完成,然后再处理回父进程。
我收到来自 post 请求的图像文件。我解析它并使用以下代码将它写入服务器...这很好。
path="/var/bla/bla/bla/"
original_fname = file1['filename']
output_file = open(os.path.join(path,original_fname), 'w')
output_file.write(file1['body'])
问题是创建文件后,我必须使用 imagemagick 命令行工具访问该文件以获取某些数据。但是使用上面的代码还为时过早,并且该过程尚未完成文件的创建。
我想在上面的代码之后做这个...
subprocess.Popen(stdout=output_file)
Popen.communicate()
但出现以下错误....
'Popen' object has no attribute '_child_created'
我该如何解决这个问题?
您没有在写入后关闭文件。你可以这样做:
output_file = open(os.path.join(path,original_fname), 'w')
output_file.write(file1['body'])
output_file.close()
但我建议使用 with
关键字,即使块中发生错误,它也会关闭文件描述符:
with open(os.path.join(path,original_fname), 'w') as output_file:
output_file.write(file1['body'])
我希望能够启动一个子进程,该子进程将文件写入服务器,然后等到它完全完成,然后再处理回父进程。 我收到来自 post 请求的图像文件。我解析它并使用以下代码将它写入服务器...这很好。
path="/var/bla/bla/bla/"
original_fname = file1['filename']
output_file = open(os.path.join(path,original_fname), 'w')
output_file.write(file1['body'])
问题是创建文件后,我必须使用 imagemagick 命令行工具访问该文件以获取某些数据。但是使用上面的代码还为时过早,并且该过程尚未完成文件的创建。 我想在上面的代码之后做这个...
subprocess.Popen(stdout=output_file)
Popen.communicate()
但出现以下错误....
'Popen' object has no attribute '_child_created'
我该如何解决这个问题?
您没有在写入后关闭文件。你可以这样做:
output_file = open(os.path.join(path,original_fname), 'w')
output_file.write(file1['body'])
output_file.close()
但我建议使用 with
关键字,即使块中发生错误,它也会关闭文件描述符:
with open(os.path.join(path,original_fname), 'w') as output_file:
output_file.write(file1['body'])