您可以采用的一种方法是使用 json 作为父脚本和子脚本之间的协议,因为 json 支持在许多语言中很容易获得,并且具有相当的表现力。您还可以使用管道将任意数量的数据发送到子进程,假设您的要求允许您从标准输入读取子脚本。例如,父母可以做类似的事情(显示 Python 2.6):
#!/usr/bin/env python
import json
import subprocess
data_for_child = {
'text' : 'Twas brillig...',
'flag1' : False,
'flag2' : True
}
child = subprocess.Popen(["./childscript"], stdin=subprocess.PIPE)
json.dump(data_for_child, child.stdin)
这是一个子脚本的草图:
#!/usr/bin/env python
# Imagine this were written in a different language.
import json
import sys
d = json.load(sys.stdin)
print d
在这个简单的示例中,输出为:
$ ./foo12.py
{u'text': u'Twas brillig...', u'flag2': True, u'flag1': False}