目前我正在使用JSON作为序列化格式,将包含字符串,数字和Ruby数组的简单哈希转移到Python脚本中:
IO.popen('./convert.py', 'w') do |w|
w.write({ :height => height, :width => width, :id => job_id, :data => pix }.to_json)
w.write "\n"
w.close_write
end
Run Code Online (Sandbox Code Playgroud)
在这种情况下height,width并且job_id都是数字,并且pix是整数数组的数组.
这个运行的python脚本是:
#!/usr/bin/env python
from PIL import Image
import json
import sys
output = json.load(sys.stdin)
width = output['width']
height = output['height']
name = 'images/' + str(output['id']) + '/image.bmp'
data = [ tuple(datum) for datum in output['data'] ]
img = Image.new("RGB", (width, height))
img.putdata(data)
img.save(name)
Run Code Online (Sandbox Code Playgroud)
使用具有390万个值的数组(可能是通常使用的大小的1/4)进行的一些快速测试显示脚本需要大约105秒,并且90秒以下所有行都被output = ...注释掉了.显然,如果序列化没有花费85%的处理时间用于这样一个简单的脚本,那将是很好的. …