随机“pythonw.exe 已停止工作"崩溃 [英] Random "pythonw.exe has stopped working" crashing

查看:118
本文介绍了随机“pythonw.exe 已停止工作"崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,

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

The code in question is the following, however it can randomly happen on other scripts too (I don't think the error lies in the code)

出于某种原因,它有时会完全随机地崩溃并弹出pythonw.exe 已停止工作",这可能是在 5 小时、24 小时或 5 天后......我不知道它为什么会崩溃.

For some reason, completely randomly it sometimes crashes and pops up that "pythonw.exe has stopped working" it could be after 5 hours, 24 hours or 5 days... I can't figure out why it's crashing.

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)

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

Does anyone know how I can find out why it's crashing or know why and know a way to fix it?

谢谢
- Hyflex

Thanks
- Hyflex

推荐答案

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

I don't know, but it may be related to the two lines that do this:

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

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

Those aren't doing what you intend them to do: they leave the file open. You need to call the close method (not merely retrieve it):

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

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

But that's probably not it. CPython will automatically close the file object when it becomes garbage (which happens immediately in this case - refcount hits 0 as soon as the statement ends).

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

Maybe you should move to a Linux system ;-)

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

Idea: would it be possible to run this with python.exe instead, from a DOS box (cmd.exe) you leave open and ignore? A huge problem with debugging pythonw.exe deaths is that there's no console window to show any error messages that may pop up.

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

Which leads to another question: what's this line doing?

print "Running..."

如果你在 pythonw.exe 下运行,你永远看不到它,对吧?而这可能会导致问题,这取决于确切地您正在运行的 Python 和 Windows 版本.标准输入标准输出并不存在,在pythonw下,我记得追踪到一个神秘的pythonw.execode> 当太多"数据被写入 sys.stdout(print 使用)时,Microsoft 库会崩溃.

If you're running under pythonw.exe, you never see it, right? And that can cause problems, depending on exactly which versions of Python and Windows you're running. Standard input and standard output don't really exist, under pythonw, and I remember tracking down one mysterious pythonw.exe death to the Microsoft libraries blowing up when "too much" data was written to sys.stdout (which print uses).

一种判断方式:如果您在 python.exe 下运行它而不是从 DOS 框运行,并且它运行了一年而没有崩溃,这可能是原因;-)

One way to tell: if you run this under python.exe instead from a DOS box, and it runs for a year without crashing, that was probably the cause ;-)

这是一个简单的程序:

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 用于运行它.

Using Python 2.7.6 under 32-bit Windows Vista, it has radically different behavior depending on whether python.exe or pythonw.exe is used to run it.

python.exe下:

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

这种情况一直持续下去,count.txt 中的值不断增加.但是:

That goes on forever, and the value in count.txt keeps increasing. But:

C:\Python27>pythonw yyy.py

C:\Python27>

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

That is, there's no visible output. And that's expected: pythonw runs its program disconnected from the console window.

在很短的时间后,pythonw.exe 无声无息地消失(使用任务管理器查看) - 消失得无影无踪.那个时候:

After a very short time, pythonw.exe silently dies (use Task Manager to see this) - vanishes without a trace. At that point:

C:\Python27>type count.txt
1025

因此,当从断开连接的程序向 stdout 写入太多"内容时,MS 的库仍然很糟糕.取出打印嗨!",它就永远"运行了.

So MS's libraries are still crapping out when "too much" is written to stdout from a disconnected program. Take out the print "hi!", and it runs "forever".

这在 Python 3 中是固定的",通过将 sys.stdout 绑定到其 pythonw.exe 下的 None 的可疑权宜之计.您可以在这里阅读这场混乱的历史.

This is "fixed" in Python 3, via the dubious expedient of binding sys.stdout to None under its pythonw.exe. You can read the history of this mess here.

这篇关于随机“pythonw.exe 已停止工作"崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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