[英]Creative stdin and error handling for python
I use a third-party program on Windows10 -- call it SuckySoftware.exe. 我在Windows10上使用第三方程序-称为SuckySoftware.exe。
This program can launch a python script and do other stuff. 该程序可以启动python脚本并执行其他操作。
When it launches a python script, unfortunately I don't get a command window launched. 不幸的是,当它启动python脚本时,我没有启动命令窗口。 This creates three problems:
这产生了三个问题:
1) I can't give it input. 1)我不能给它输入。 For example using python3 and having "input()" as one of the commands causes SuckySoftware.exe to hang, because it is waiting for input into some non-existent command window.
例如,使用python3并将“ input()”作为命令之一会导致SuckySoftware.exe挂起,因为它正在等待某些不存在的命令窗口中的输入。
2) I can't get output. 2)我无法获得输出。 So "print('hello')" doesn't show up anywhere
因此,“ print('hello')”不会显示在任何地方
3) There is no error handling. 3)没有错误处理。 So if I have a ValueError in my python code, it silently fails on that line and doesn't execute the rest of the python.
因此,如果我的python代码中存在ValueError,则该行将在该行静默失败,并且不会执行其余的python。
I've solved #2 by doing: 我已经通过执行以下操作解决了#2:
sys.stdout = open(logfile, 'wb', 0)
sys.stdout.write('hello'.encode('utf-8'))
and then using mTail to monitor logfile in real-time. 然后使用mTail实时监视日志文件。
I can't figure out how to solve 1 or 3. 我不知道如何解决1或3。
For 1, I wonder if there is a simple way of launching a simple GUI with one textbox. 对于1,我想知道是否有一种简单的方法来启动带有一个文本框的简单GUI。 Whenever my logfile shows "Please enter a value" then I could enter it in to that textbox and press enter, and it would take it on stdin.
每当我的日志文件显示“请输入一个值”时,我都可以将其输入到该文本框中,然后按Enter键,它将在stdin上显示。 However this seems overly complicated.
但是,这似乎过于复杂。
For 3, I wonder if I can catch all error messages (including the line number it failed on) and print them to stdout before the script quits. 对于3,我想知道是否可以捕获所有错误消息(包括失败的行号)并在脚本退出之前将它们打印到stdout。
Ideas? 想法?
First, do open a file as sys.stderr
, since output there may be useful in any event. 首先,请打开文件
sys.stderr
,因为在那里的输出在任何情况下都可能有用。
sys.stdin
to a (TCP, aka SOCK_STREAM
) socket connection to another process. sys.stdin
设置为与另一个进程的(TCP,又名SOCK_STREAM
)套接字连接。 sys.stdout
and/or sys.stderr
as well, although then the helper process has to use select
to handle the multiple streams without deadlock. sys.stdout
和/或sys.stderr
这样的套接字,尽管那样,辅助进程必须使用select
来处理多个流而不会出现死锁。 (You might want to use a wrapper as sys.stderr
to preserve its unbuffered status.) sys.stderr
来保留其未缓冲状态。) sys.excepthook
that reports the exception via (your) stderr
. sys.excepthook
分配一个函数,该函数通过(您的) stderr
报告异常。 (There is perhaps something already assigning such a function, since nothing shows up on sys.stderr
as it is.) sys.stderr
上没有任何显示。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.