如何防止 subprocess.popen “迷路”在 Windows 中?
我有一个奇怪的 Python(3.x) 问题:
基本上我只是想创建一个计时器,它在预定义的时间调用外部程序(.exe) - (下面简化,另外还有一个倒计时输出):
time.sleep(y)
x=popen("pathto.exe")
如果我测试我的代码,一切都会完全按照我想要的方式工作,但仅适用于小 y 。对于大的 y(等待超过几个小时),Python 不会执行 popen 命令(但也没有任何错误)。 popen 命令之后的任何其他“正常”代码(例如我的情况下的电子邮件通知)都可以正常工作。
我不完全确定这是 Windows(我在 64 位 Windows 7 上工作,禁用了我知道的所有节能功能)还是 Python 问题,但到目前为止找不到解决方案:
我尝试了 popen 的几个附加参数(Shell/no Shell 等),并尝试在等待期之前打开程序/定义 X,然后再次关闭它,但不幸的是没有解决这个问题。
非常感谢!
编辑:更详细的代码示例:
while 1:
if time.mktime(trade2)<=time.time():
break
else:
dif=time.mktime(trade2)-time.time() # this is just for a visible countdown
aus=time.gmtime(dif)
sys.stdout.flush()
print("\b\b\b\b\b\b\b\b\b", end="\r")
print(aus[3],aus[4],aus[5], sep=":",end="")
time.sleep(0.5)
x=subprocess.Popen("c:/xyz/abc.exe")
print("Sending Mail...")
print() 始终按预期执行,仅当倒计时小于约 1-2 小时时才执行 Popen。
I have a somehow curious Python(3.x) problem:
Basically i just want to create a timer which calls an external program (.exe) at a predefined time - (simplified below, additionally there is a countdown output):
time.sleep(y)
x=popen("pathto.exe")
If i test my code everything works exactly as i want BUT only for small y. For large y (wait for more than a few hours), Python doesnt execute the popen command (but there arent any Errors as well). Any other "normal" code after the popen command (for example an Email notification in my case) works fine.
Im not entirely sure if this is a Windows (im working on 64bit Windows 7, disabled all energy saving features im aware of) or a Python problem but couldnt find a solution so far:
I tried several additional arguments for popen (Shell/no Shell, etc.) and tried to open the program/define X ahead of the waiting period and close it afterwards again but nothing solved this unfortunately.
Many Thanks!
Edit: more detailed code example:
while 1:
if time.mktime(trade2)<=time.time():
break
else:
dif=time.mktime(trade2)-time.time() # this is just for a visible countdown
aus=time.gmtime(dif)
sys.stdout.flush()
print("\b\b\b\b\b\b\b\b\b", end="\r")
print(aus[3],aus[4],aus[5], sep=":",end="")
time.sleep(0.5)
x=subprocess.Popen("c:/xyz/abc.exe")
print("Sending Mail...")
The print() is ALWAYS executed as expected, the Popen only when the countdown less than approx 1-2hours.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
睡眠没有保证的醒来时间。 根据文档睡眠可能会持续一段时间比要求的时间长或短。我之前已经回答过这个问题,并有其他选择。
Sleep does not have a guaranteed wake up time. According to the docs sleep may last a longer or shorter amount of time than requested. I've answered this question before on SO, with alternatives.