PyDev/Python3.1 子进程错误 - 未声明编码
当我运行 Parent.py 调用子进程 child.exe 时,会发生以下错误
File "child.exe", line 1
SyntaxError: Non-UTF-8 code starting with '\x90' in file child.exe on line 1, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
如果我使用 child.py 运行 Parent.py,则执行成功返回 'Hello from家长。来自日食。
如果我从附带的 IDLE 运行 Parent.py,则在 child.exe 和 child.py 的情况下都不会返回任何内容
我已阅读http://python.org/dev/peps/pep-0263 / 文档并理解它,可能误解它意味着我应该添加我尝试添加到 child.exe 中的建议注释......它们不起作用。
Parent.py
import os
import subprocess
import sys
if sys.platform == "win32":
import msvcrt
import _subprocess
else:
import fcntl
# Create pipe for communication
pipeout, pipein = os.pipe()
# Prepare to pass to child process
if sys.platform == "win32":
curproc = _subprocess.GetCurrentProcess()
pipeouth = msvcrt.get_osfhandle(pipeout)
pipeoutih = _subprocess.DuplicateHandle(curproc, pipeouth, curproc, 0, 1,
_subprocess.DUPLICATE_SAME_ACCESS)
pipearg = str(int(pipeoutih))
else:
pipearg = str(pipeout)
# Must close pipe input if child will block waiting for end
# Can also be closed in a preexec_fn passed to subprocess.Popen
fcntl.fcntl(pipein, fcntl.F_SETFD, fcntl.FD_CLOEXEC)
# Start child with argument indicating which FD/FH to read from
subproc = subprocess.Popen(['python', 'child.exe', pipearg], close_fds=False)
# Close read end of pipe in parent
os.close(pipeout)
if sys.platform == "win32":
pipeoutih.Close()
# Write to child (could be done with os.write, without os.fdopen)
pipefh = os.fdopen(pipein, 'w')
pipefh.write("Hello from parent.")
pipefh.close()
# Wait for the child to finish
subproc.wait()
Child.exe(使用 cx_freeze 冻结)
import os, sys
if sys.platform == "win32":
import msvcrt
# Get file descriptor from argument
pipearg = int(sys.argv[1])
if sys.platform == "win32":
pipeoutfd = msvcrt.open_osfhandle(pipearg, 0)
else:
pipeoutfd = pipearg
# Read from pipe
# Note: Could be done with os.read/os.close directly, instead of os.fdopen
pipeout = os.fdopen(pipeoutfd, 'r')
print(pipeout.read())
pipeout.close()
When I run Parent.py which calls the subprocess child.exe the following error occurs
File "child.exe", line 1
SyntaxError: Non-UTF-8 code starting with '\x90' in file child.exe on line 1, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
If I run the Parent.py with child.py, the execution successfully returns 'Hello from parent.' from Eclipse.
If I run Parent.py from the shipped IDLE nothing is returned in the case of both child.exe and child.py
I have read the http://python.org/dev/peps/pep-0263/ documentation and understood it, possibly missunderstood it to mean I should add the suggested comments which I have tried adding to child.exe ... they did not work.
Parent.py
import os
import subprocess
import sys
if sys.platform == "win32":
import msvcrt
import _subprocess
else:
import fcntl
# Create pipe for communication
pipeout, pipein = os.pipe()
# Prepare to pass to child process
if sys.platform == "win32":
curproc = _subprocess.GetCurrentProcess()
pipeouth = msvcrt.get_osfhandle(pipeout)
pipeoutih = _subprocess.DuplicateHandle(curproc, pipeouth, curproc, 0, 1,
_subprocess.DUPLICATE_SAME_ACCESS)
pipearg = str(int(pipeoutih))
else:
pipearg = str(pipeout)
# Must close pipe input if child will block waiting for end
# Can also be closed in a preexec_fn passed to subprocess.Popen
fcntl.fcntl(pipein, fcntl.F_SETFD, fcntl.FD_CLOEXEC)
# Start child with argument indicating which FD/FH to read from
subproc = subprocess.Popen(['python', 'child.exe', pipearg], close_fds=False)
# Close read end of pipe in parent
os.close(pipeout)
if sys.platform == "win32":
pipeoutih.Close()
# Write to child (could be done with os.write, without os.fdopen)
pipefh = os.fdopen(pipein, 'w')
pipefh.write("Hello from parent.")
pipefh.close()
# Wait for the child to finish
subproc.wait()
Child.exe (frozen with cx_freeze)
import os, sys
if sys.platform == "win32":
import msvcrt
# Get file descriptor from argument
pipearg = int(sys.argv[1])
if sys.platform == "win32":
pipeoutfd = msvcrt.open_osfhandle(pipearg, 0)
else:
pipeoutfd = pipearg
# Read from pipe
# Note: Could be done with os.read/os.close directly, instead of os.fdopen
pipeout = os.fdopen(pipeoutfd, 'r')
print(pipeout.read())
pipeout.close()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是,如果 child.exe 是普通的 Windows 可执行文件,那么您正试图让 python 读取二进制文件。出现此错误是因为二进制文件的字节不符合标准 ascii - utf8 标准,因此解释器无法读取它。
也许您想要的只是执行 child.exe,然后从中删除 python 行:
The problem is that you are trying to make python read a binary file, if child.exe is a normal windows executable. The error raises because the bytes of the binary files are out of the standard ascii - utf8 standard so the interpreter can't read it.
Maybe what you want is to just execute the child.exe just remove the python line from it: