如何在Ruby和Python之间传输基本对象?

Nem*_*157 6 ruby python serialization

目前我正在使用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%的处理时间用于这样一个简单的脚本,那将是很好的.

加速我能想到的唯一方法是找到某种形式的二进制序列化/编组,可用于将数据从Ruby传输到Python.不幸的是,我一直无法找到任何这样的系统,只有RMarshal似乎能够以其他方式工作.

t6d*_*t6d 9

也许MessagePack是当时的方式.存在多种语言的绑定,包括Ruby和Python.