Python程序意外终止 [英] Python program terminating unexpectedly

查看:36
本文介绍了Python程序意外终止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序可以抓取网站并在找到它时下载文件.通常它运行得很好,但有时它会在完成搜索序列之前完全终止程序的操作.我难住了.仅在搜索时下载,它永远不会退出.我目前正在猜测套接字错误问题,但就像我上面所说的那样,我很难过.我已经进行了 httperror 检查,但没有为 http 错误代码显示任何内容.我现在正在尝试插入套接字错误检查,它变得更加疯狂.在为套接字错误检查添加任何内容之前,它工作正常.一旦我添加了套接字错误检查,程序甚至不会运行.它会调出 IDLE 显示并显示光标,就像 IDLE 已准备好并等待下一个命令一样.否则,如果没有套接字错误检查,则表明程序正在运行,直到 tkinter 窗口关闭(错误程序意外终止).如果 tkinter 窗口关闭,则不会在 IDLE 上出现任何错误.

I have a program that scrapes a website and downloads files when it finds it. Often it runs just fine but at other times it flat out terminates the operation of the program before it is finishing searching the sequence. I'm stumped. It never quits while downloading only while searching. I'm currently guessing a socket error problem but like I said above I'm stumped. I've put in httperror checking and nothing gets displayed for an http error code. I'm trying right now to insert socket error checking and it gets even crazier. Prior to adding anything for socket error checking it works fine. Once I add in the socket error checking the program won't even run. It brings up the IDLE display and shows the cursor, like IDLE is ready and waiting for the next command. Otherwise without socket error checking it indicates that the program is running until either the tkinter window shuts down(err program terminates unexpectedly). If the tkinter window shuts downs it doesn't give any error on IDLE.

我该怎么做才能找出这个程序有时会提前终止的原因,并能够将其捕获,这样它就不会终止,只需返回并再次运行相同的网址即可.我想我已经处理了重新运行相同的网址,但我没有正确处理套接字错误,如果它甚至是套接字错误问题.我被难住了.

What do I have to do to find out why this program is terminating early at times and be able to trap it out so it won't terminate and just go back and rerun the same web address again. I think I have the rerunning the same web address taken care of but I don't have the socket error handling correct, if it's even socket error trouble. I'm stumped.

#!/usr/bin/python3.4

import urllib.request
import os
from tkinter import *
import time
import urllib.error
import errno

root = Tk()
root.title("photodownloader")
root.geometry("200x200")

app = Frame(root)
app.grid()

os.chdir('/home/someone/somewhere/')
Fileupdate = 10000
Filecount = 19999
while Fileupdate <= Filecount:
        try:
                root.title(Fileupdate)
                url = 'http://www.webpage.com/photos/'+str(Fileupdate)+'.jpg'
                a = urllib.request.urlopen(url)
                urllib.request.urlretrieve(url, str(Fileupdate)+'.jpg')
        except urllib.error.HTTPError as err:
                if err.code == 404:
                        Fileupdate = Fileupdate + 1
                        root.update_idletasks()
                        continue
                else:
                        print(err.code)
                        Fileupdate = Fileupdate + 1
                        root.update_idletasks()
                        continue
        except socket.error, v:
                print(Fileupdate, v[0])
                continue

        Fileupdate = Fileupdate+1
        root.update_idletasks()

推荐答案

我认为问题是由于 tkinter 没有机会启动它的主事件循环,当您调用 root.mainloop(),我建议将您当前在 while 循环中的代码改为使用 root.after() 方法定期调用的函数.我已经包含了一个潜在的更改来测试这是否会解决问题.

I think the problem is caused by tkinter not given the chance to start it's main event loop which is done when you call root.mainloop(), I'd recommend making the code you currently have in a while loop instead to be a function that is periodically called with the root.after() method. I have included a potential change to test if this would fix the issue.

注意以下几行:

                    Fileupdate = Fileupdate + 1
                    root.update_idletasks()
                    continue

在某些 except 分支中是多余的,因为如果代码继续运行就会发生这种情况,因此修改代码以在函数中工作的一部分就是简单地摆脱这些部分.这是我希望您尝试从原始 while 语句开始运行的代码:

in some except branches are redundant since that would happen if the code kept going anyway, so part of modifying the code to work in a function was to simply get rid of those parts. Here is the code I'd like you to try running starting from the original while statement:

#-while Fileupdate <= Filecount:
def UPDATE_SOCKET():
    global Fileupdate #allow the global variable to be changed
    if Fileupdate <= Filecount:
#/+
        try:
                root.title(Fileupdate)
                url = 'http://www.webpage.com/photos/'+str(Fileupdate)+'.jpg'
                a = urllib.request.urlopen(url)
                urllib.request.urlretrieve(url, str(Fileupdate)+'.jpg')
        except urllib.error.HTTPError as err:
                #<large redundant section removed>
                print("error code",err.code)
        except socket.error as v:
                print("socket error",Fileupdate, v[0])
#-                continue
                root.after(500, UPDATE_SOCKET)
                return
#/+


        Fileupdate = Fileupdate+1
#-        root.update_idletasks()
        root.after(100, UPDATE_SOCKET) #after 100 milliseconds call the function again
        #change 100 to a smaller time to call this more frequently.


root.after(0,UPDATE_SOCKET) #when it has a chance, call the update for first time

root.mainloop() #enter main event loop
#/+

我用 #- 表示改变的行,后跟替换它的块,以 #/+

I indicate changed lines with a #- followed by the chunk that replaces it ending with a #/+

这篇关于Python程序意外终止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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