python - 如何在 Python 中使用队列进行线程处理时处理异常
问题描述
我正在进行大量下载,可以从中引发任意数量的异常。我的代码似乎可以工作,但不确定它是否适用于所有情况。
我在下面有我的基本结构,它在重载的线程类中捕获一个错误,然后在该类中设置一个标志,并让任何剩余的队列项在不采取任何行动的情况下循环出去。不应中断已经运行的线程。
下面的结构中是否存在潜在错误或可以改进?
以前问过的问题:如何在 Python 中使用队列处理线程中的异常?
class DownloadWorker(Thread):
my_error_message = None # Critical Error Flag
def __init__(self, queue):
Thread.__init__(self)
self.queue = queue
def run(self):
while True:
thread_name = self.queue.get()
if DownloadWorker.my_error_message:
self.queue.task_done() # One by one, this will terminate remaining items in queue
else:
try:
self.dummy_download_data(thread_name) # Process something here
except Exception as e:
DownloadWorker.my_error_message = "Fatal Error in " + str(e) # omitted lock/release
finally:
self.queue.task_done()
def dummy_download_data(self, thread_name):
if thread_name == 'Thread2': # mimic some error in some function way deep down
raise Exception("? My Problem with " + thread_name)
else:
time.sleep(2)
main()
thread_list = ['Thread1', 'Thread2', 'Thread3', 'Thread4', 'Thread5']
queue = Queue()
for x in range(3):
worker = DownloadWorker(queue)
worker.daemon = True
worker.start()
for name in thread_list: # Better way to handle exceptions thown in a thread?
queue.put(name)
queue.join()
if DownloadWorker.my_error_message: # Handling my error
print(DownloadWorker.my_error_message)
print('fini')
解决方案
推荐阅读
- sql - Oracle 间隔 1 天
- angular - Angular 项目生产版本在导入谷歌字体时出现问题
- c# - 将列表(对象)转换为对象
- c# - 在 C# 中从 mongoDB 数据库中采样一个随机文档
- python - 使用 Selenium 从网站中抓取文本
- android - Firebase Analytics 未显示 iOS 应用的用户参与度
- r - 如何使用 dim(x) 修复 fitafirma 错误?
- vega - Vega-lite 图表,用于具有动态 timeUnit 分箱的可缩放时间线直方图
- flutter - 在颤振中为表单域和控制器构建一个包装器小部件
- elasticsearch - Filebeat 未将日志转发到 AWS 中的 Elasticsearch 服务