Python 如何使其与Windows兼容?
你好,斯塔克 我在将我的一个Python Linux脚本移植到Windows时遇到了一个小(大)问题。关于这一点,令人毛骨悚然的是,我必须启动一个进程,将它的所有流重定向到我在脚本中读写的管道中 对于Linux,这是小菜一碟:Python 如何使其与Windows兼容?,python,pipe,subprocess,portability,Python,Pipe,Subprocess,Portability,你好,斯塔克 我在将我的一个Python Linux脚本移植到Windows时遇到了一个小(大)问题。关于这一点,令人毛骨悚然的是,我必须启动一个进程,将它的所有流重定向到我在脚本中读写的管道中 对于Linux,这是小菜一碟: server_startcmd = [ "java", "-Xmx%s" % self.java_heapmax, "-Xms%s" % self.java_heapmin, "-
server_startcmd = [
"java",
"-Xmx%s" % self.java_heapmax,
"-Xms%s" % self.java_heapmin,
"-jar",
server_jar,
"nogui"
]
server = Popen(server_startcmd, stdout = PIPE,
stderr = PIPE,
stdin = PIPE)
outputs = [
server_socket, # A listener socket that has been setup before
server.stderr,
server.stdout,
sys.stdin # Because I also have to read and process this.
]
clients = []
while True:
read_ready, write_ready, except_ready = select.select(outputs, [], [], 1.0)
if read_ready == []:
perform_idle_command() # important step
else:
for s in read_ready:
if s == sys.stdin:
# Do stdin stuff
elif s == server_socket:
# Accept client and add it to 'clients'
elif s in clients:
# Got data from one of the clients
脚本中最重要的部分是在服务器套接字、脚本的stdin和子进程的输出通道(以及输入通道,我的脚本将写入该通道,尽管该通道不在select()列表中)之间的整个3路交替
我知道Windows的Win32 API模块中有Win32管道。问题是,找到这个API的资源非常困难,而我所发现的并没有真正的帮助
如何利用此win32pipe模块执行我想要的操作?我有一些来源,在不同但相似的情况下使用它,但这让我非常困惑:
if os.name == 'nt':
import win32pipe
(stdin, stdout) = win32pipe.popen4(" ".join(server_args))
else:
server = Popen(server_args,
stdout = PIPE,
stdin = PIPE,
stderr = PIPE)
outputs = [server.stderr, server.stdout, sys.stdin]
stdin = server.stdin
[...]
while True:
try:
if os.name == 'nt':
outready = [stdout]
else:
outready, inready, exceptready = select.select(outputs, [], [], 1.0)
except:
break
stdout
这里是用win32pipe.popen4(…)
这些问题包括:
- 为什么不为windows版本选择()?那不行吗
- 如果您不在那里使用
,我如何实现select()
提供的必要超时(这里显然不能这样工作)select()
请帮帮我 我认为不能在管道上使用select()。 在其中一个项目中,我正在将linux应用程序移植到Windows,我也错过了这一点,不得不重写整个逻辑