如何防止subprocess.popen“迷路"?在Windows中? [英] How to prevent subprocess.popen to "get lost" in Windows?

查看:122
本文介绍了如何防止subprocess.popen“迷路"?在Windows中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个奇怪的Python(3.x)问题:
基本上我只想创建一个计时器,该计时器在预定义的时间调用外部程序(.exe)-(在下面进行了简化,此外还有一个倒计时输出):

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")

如果我测试我的代码,则所有功能都完全可以按我想要的方式工作,但仅适用于小y.对于较大的y(等待多个小时),Python不会执行popen命令(但也不会出现任何错误). popen命令之后的任何其他正常"代码(例如,在我的情况下为电子邮件通知)都可以正常工作.

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.

我不确定是Windows(在64位Windows 7上运行,即禁用了我所知道的所有节能功能)还是Python问题,但目前还找不到解决方案:
我尝试了一些其他的popen参数(Shell/no Shell等),并尝试在等待期之前打开程序/定义X,然后再关闭它,但不幸的是,没有任何解决方案.

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.

非常感谢!

更详细的代码示例:

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.

The print() is ALWAYS executed as expected, the Popen only when the countdown less than approx 1-2hours.

推荐答案

睡眠没有保证的唤醒时间. 根据文档,睡眠可能会持续一个比要求的时间更长或更短的时间.在SO上,我已经回答了这个问题,并选择了其他方法.

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.

这篇关于如何防止subprocess.popen“迷路"?在Windows中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆