【问题标题】:Random "pythonw.exe has stopped working" crashing随机“pythonw.exe 已停止工作”崩溃
【发布时间】:2013-11-29 22:51:56
【问题描述】:

所以,

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

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

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)

有谁知道我如何找出它崩溃的原因或知道为什么并知道修复它的方法?

谢谢
- 海福克斯

【问题讨论】:

  • 顺便问一下,您使用的是哪个版本的 Python 2.7?并且,如果可以,请尝试迁移到最新发布的版本。
  • 我正在运行 2.7.5 版

标签: python python-2.7


【解决方案1】:

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

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

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

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

但可能不是这样。当文件对象变成垃圾时,CPython 会自动关闭文件对象(在这种情况下会立即发生 - 语句结束时引用计数为 0)。

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

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

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

print "Running..."

如果你在pythonw.exe 下运行,你永远看不到它,对吧?这可能会导致问题,具体取决于确切您正在运行的 Python 和 Windows 版本。 Standard inputstandard outputpythonw 下并不真正存在,我记得追查过一个神秘的 pythonw.exe 死亡事件,当“太多”数据写入 sys.stdout(其中 @ 987654334@ 使用)。

一种判断方法:如果您在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

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

Python 3

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

【讨论】:

  • 您选择的第一点肯定是错误,但第二点不是错误,我在黑色 cmd 提示符下运行它,但我猜崩溃源于下标,我将尝试在 .close() 修复后运行几天,看看效果如何。
  • 不管你是从 DOS 框运行pythonw.exe:尾随的w 表示程序运行已断开从控制台。任何发送到标准输出或标准错误的错误信息都会消失。您必须使用python.exe(没有尾随w)来避免这种情况。
  • @Hyflex,见我刚才编辑结束时的示例程序。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-26
  • 1970-01-01
  • 1970-01-01
  • 2014-04-11
  • 2014-09-10
  • 1970-01-01
相关资源
最近更新 更多