所以,
有问题的代码如下,但它也可以随机发生在其他脚本上(我不认为错误在于代码)
出于某种原因,完全随机的它有时会崩溃并弹出"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)
Run Code Online (Sandbox Code Playgroud)
有谁知道我怎么能找出它为什么崩溃或知道原因并知道解决方法?
谢谢
- Hyflex
我不知道,但它可能与两行有关:
open("tca.txt", 'w').close
Run Code Online (Sandbox Code Playgroud)
那些没有做你打算做的事情:他们把文件保持打开状态.您需要调用 close方法(不仅仅是检索它):
open("tca.txt", 'w').close()
^^
Run Code Online (Sandbox Code Playgroud)
但那可能不是它.CPython会在变为垃圾时自动关闭文件对象(在这种情况下立即发生 - 一旦语句结束,refcount就会命中0).
也许你应该转移到Linux系统;-)
想法:是否有可能运行它python.exe而不是从DOS框(cmd.exe)你打开并忽略?调试pythonw.exe死机的一个巨大问题是,没有控制台窗口可以显示可能弹出的任何错误消息.
这引出了另一个问题:这条线在做什么?
print "Running..."
Run Code Online (Sandbox Code Playgroud)
如果你在奔跑pythonw.exe,你永远不会看到它,对吧?而这可能会导致问题,取决于准确的Python和Windows你正在运行的哪个版本. Standard input并且standard output真的不存在,pythonw我记得pythonw.exe当"太多"数据被写入sys.stdout(print使用)时,追踪一个神秘的死亡微软库爆炸.
一种说法:如果你在python.exeDOS框下运行它,它运行一年而不会崩溃,这可能是原因;-)
这是一个简单的程序:
i = 0
while 1:
i += 1
with open("count.txt", "w") as f:
print >> f, i
print "hi!"
Run Code Online (Sandbox Code Playgroud)
使用Python 2.7.6在32位Windows Vista,它从根本上取决于是否不同的行为python.exe或者pythonw.exe是用来运行它.
下python.exe:
C:\Python27>python yyy.py
hi!
hi!
hi!
hi!
hi!
hi!
hi!
hi!
hi!
hi!
hi!
...
Run Code Online (Sandbox Code Playgroud)
这种情况将永远count.txt持续下去,并且价值不断增加.但:
C:\Python27>pythonw yyy.py
C:\Python27>
Run Code Online (Sandbox Code Playgroud)
也就是说,没有可见的输出.这是预期的: pythonw运行程序与控制台窗口断开连接.
在很短的时间后,pythonw.exe静静地死亡(使用任务管理器看到这一点) - 消失得无影无踪.在那时候:
C:\Python27>type count.txt
1025
Run Code Online (Sandbox Code Playgroud)
因此,当断开连接的程序向stdout写入"太多"时,MS的库仍然会被淘汰.取出print "hi!"它,它"永远"运行.
这在Python 3中是"固定的",通过绑定sys.stdout到None它下面的可疑权宜之计pythonw.exe.你可以在这里阅读这个烂摊子的历史.
| 归档时间: |
|
| 查看次数: |
8446 次 |
| 最近记录: |