随机“pythonw.exe已停止工作”崩溃

时间:2013-11-29 22:51:57

标签: python python-2.7

SO,

有问题的代码如下,但它也可以随机发生在其他脚本上(我不认为错误在于代码)

出于某种原因,完全随机的它有时会崩溃并弹出“pythonw.exe已经停止工作”它可能是在5小时,24小时或5天之后......我无法弄清楚它为什么会崩溃。< / p>

from datetime import date, timedelta
from sched import scheduler
from time import time, sleep, strftime
import random
import traceback

s = scheduler(time, sleep)
random.seed()

def periodically(runtime, intsmall, intlarge, function):

    currenttime = strftime('%H:%M:%S')

    with open('eod.txt') as o:
        eod = o.read().strip()
        if eod == "1":
            EOD_T = True
        else:
            EOD_T = False

    while currenttime >= '23:40:00' and currenttime <= '23:59:59' or currenttime >= '00:00:00' and currenttime <= '11:30:00' or EOD_T:
        if currenttime >= '23:50:00' and currenttime <= '23:59:59':
            EOD_T = False
        currenttime = strftime('%H:%M:%S')
        print currenttime, "Idling..."
        sleep(10)
        open("tca.txt", 'w').close

    open("tca.txt", 'w').close

    runtime += random.randrange(intsmall, intlarge)
    s.enter(runtime, 1, function, ())
    s.run()

def execute_subscripts():

    st = time()
    print "Running..."

    try:
       with open('main.csv'):
           CSVFile = True
    except IOError:
        CSVFile = False

    with open('eod.txt') as eod:
        eod = eod.read().strip()
        if eod == "1":
            EOD_T = True
        else:
            EOD_T = False

    if CSVFile and not EOD_T:
        errors = open('ERROR(S).txt', 'a')

        try:
            execfile("SUBSCRIPTS/test.py", {})
        except Exception:
            errors.write(traceback.format_exc() + '\n')
            errors.write("\n\n")

        errors.close()

    print """ %.3f seconds""" % (time() - st)

while True:
    periodically(15, -10, +50, execute_subscripts)

有谁知道我怎么能找出崩溃或知道原因的原因并知道修复方法?

感谢
- Hyflex

1 个答案:

答案 0 :(得分:7)

我不知道,但可能与执行此操作的两行有关:

open("tca.txt", 'w').close

那些没有做你打算做的事情:他们让文件保持打开状态。您需要调用 close方法(不仅仅是检索它):

open("tca.txt", 'w').close()
                          ^^

但那可能不是。 CPython会在变为垃圾时自动关闭文件对象(在这种情况下立即发生 - 一旦语句结束,refcount就会命中0)。

也许你应该转移到Linux系统; - )

想法:是否可以使用python.exe运行此代码,从DOS框(cmd.exe)开始并忽略?调试pythonw.exe死亡的一个巨大问题是,没有控制台窗口可以显示可能弹出的任何错误消息。

这引出了另一个问题:这条线在做什么?

print "Running..."

如果你在pythonw.exe下运行,你永远不会看到它,对吗?并且可以导致问题,具体取决于 您正在运行的Python和Windows版本。在Standard input下,standard outputpythonw并不存在,而且我记得当“太多”数据出现时微软库中的一个神秘pythonw.exe死亡写入sys.stdoutprint使用)。

一种说法:如果你在python.exe下运行它,而不是从DOS框运行,它运行一年而不会崩溃,这可能是原因; - )

实施例

这是一个简单的程序:

i = 0
while 1:
    i += 1
    with open("count.txt", "w") as f:
        print >> f, i
    print "hi!"

在32位Windows Vista下使用Python 2.7.6,它具有根本不同的行为,具体取决于是使用python.exe还是pythonw.exe来运行它。

python.exe下:

C:\Python27>python yyy.py
hi!
hi!
hi!
hi!
hi!
hi!
hi!
hi!
hi!
hi!
hi!
...

这种情况永远存在,count.txt中的值不断增加。但是:

C:\Python27>pythonw yyy.py

C:\Python27>

也就是说,没有可见的输出。这是预期的:pythonw从控制台窗口运行其程序已断开连接

在很短的时间之后,pythonw.exe无声地死亡(使用任务管理器看到这一点) - 消失得无影无踪。那时:

C:\Python27>type count.txt
1025

因此,当断开连接的程序向stdout写入“太多”时,MS的库仍然存在。取出print "hi!",它会“永远”运行。

Python 3

这在Python 3中是“固定的”,通过在其sys.stdout下将None绑定到pythonw.exe的可疑权宜之计。您可以阅读the history of this mess here