多个python子线程中的某个异常退出,如何让主线程继续它的工作,或者记录下被耽搁的工作?

 小情冒_256 发布于 2022-10-28 21:08

如下是一个爬虫的部分代码,现在碰到的问题是,多个线程(比如10个)中的某个在爬取、解析某个页面或者存储数据的过程中发生了异常,子线程的工作代码虽然捕捉了这一异常,但无法继续工作。

我希望最好有办法能不让线程退出,而继续分配给他的工作;退而求其次,可以开启一个新的线程继续他的工作,但这需要发生异常的线程将退出节点的信息返回给主线程;最次的,异常退出的线程应该将退出节点的信息返回并让主线程记录,方便我后面单独处理。

我不知道最佳诉求是否可以实现,或者如果不能实现,怎样可以将发生异常的线程的关键信息返回,我看了python 主线程捕获子线程异常这篇文章,大约知道思路,但他的示例中只开启了一个单独的线程,而我开始了多个线程,有一些特殊性,然后就不知道怎么处理了。

请问最优方案是否有解,以及如何获取多个子线程中某个发生异常的子线程的返回信息呢?

def getAllItems(startNum, endNum, tcount = 10):
    threads = []
    for torder in range(tcount):
        t = MyThread(torder, tcount, startNum, endNum)
        t.start()
        threads.append(t)
    for t in threads:
        t.join()
    dbUtils.closeCur()
    dbUtils.closeConn()
    print "INFO: cursor and connection closed, work done!"

class MyThread(threading.Thread):
    def __init__(self, torder, tcount, startNum, endNum):
        threading.Thread.__init__(self)
        self.torder = torder
        self.tcount = tcount
        self.startNum = startNum
        self.endNum = endNum
    
    def run(self):
        try:
            for i in range(self.startNum+self.torder, self.endNum+1, self.tcount):
                item = downloadItem(i, htmlDownloader, logFile)
                if item:
                    if databaseMutex.acquire():
                        try:
                            dbUtils.cur.execute(sql, item)
                            print "INFO: item #" + str(i) + " saved"
                            databaseMutex.release()
                        except Exception,e:
                            databaseMutex.release() # prevent work blocked
                            # raise e
        except Exception,e:
            excepinfo = "Error: can not save item #" + str(i) + " to database, " + str(e)
            print excepinfo
            if logFileMutex.acquire():
                with codecs.open(logFile, 'a', 'utf-8') as f:
                    f.write(excepinfo + "\n")
                logFileMutex.release()
1 个回答
  • 一个线程和多个线程其实道理一样,代码可以改成如下:
    def getAllItems(startNum, endNum, tcount = 10):

    threads = []
    for torder in range(tcount):
        t = MyThread(torder, tcount, startNum, endNum)
        threads.append(t)
    **try:
        for t in threads:
            t.start()
            t.join()
    except Exception, e:
        print t. exc_traceback**
    dbUtils.closeCur()
    dbUtils.closeConn()
    print "INFO: cursor and connection closed, work done!"
    2022-10-29 22:42 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有