python:如果更改sys.stdin则会发生pickle / rsh错误

时间:2015-03-31 05:08:34

标签: python stdin

inprocstd.py

import os
import sys
import cPickle as pickle

mystdin = os.pipe()
mystdout = os.pipe()

myrsh = '/usr/bin/rsh'
host = 'host1'
myexe = 'exec'
mypy = '/usr/bin/python2.5'
do = '/u/e/python/inprocb.py'
args = [myrsh, host, myexe, mypy, do]

#r,w = os.pipe()
#sys.stdin = os.fdopen(r, 'r')

pid = os.fork()
if not pid:
    try:
        os.close(mystdin[1])
        os.dup2(mystdin[0], sys.stdin.fileno())
        os.close(mystdin[0])

        os.close(mystdout[0])
        os.dup2(mystdout[1], sys.stdout.fileno())
        os.close(mystdout[1])

        os.execv(args[0], args)

    except os.error, err:
        sys.exit(1)

os.close(mystdin[0])
os.close(mystdout[1])

try:
    with os.fdopen(mystdin[1], "w") as pkl:
        pickle.dump("hi abc", pkl)

except Exception:
    pass

inprocb.py

import cPickle as pickle
import sys

msg = pickle.load(sys.stdin)
f = open('/u/e/python/myfile', 'w')
f.write(msg)
f.close()

上面的代码工作正常。但如果我取消注释该行以将stdin更改为fdopen,我会收到此错误:

Traceback (most recent call last):
  File "/u/e/python/inprocb.py", line 4, in <module>
    msg = pickle.load(sys.stdin)
EOFError

是否有可能让pickle工作并通过os.pipe()更改sys.stdin?

0 个答案:

没有答案