python捕捉线程异常_在Python中的调用者线程中捕获一个线程的异常

I'm very new to Python and multithreaded programming in general. Basically, I have a script that will copy files to another location. I would like this to be placed in another thread so I can output .... to indicate that the script is still running.

The problem that I am having is that if the files cannot be copied it will throw an exception. This is ok if running in the main thread; however, having the following code does not work:

try:

threadClass = TheThread(param1, param2, etc.)

threadClass.start() ##### **Exception takes place here**

except:

print "Caught an exception"

In the thread class itself, I tried to re-throw the exception, but it does not work. I have seen people on here ask similar questions, but they all seem to be doing something more specific than what I am trying to do (and I don't quite understand the solutions offered). I have seen people mention the usage of sys.exc_info(), however I do not know where or how to use it.

All help is greatly appreciated!

EDIT: The code for the thread class is below:

class TheThread(threading.Thread):

def __init__(self, sourceFolder, destFolder):

threading.Thread.__init__(self)

self.sourceFolder = sourceFolder

self.destFolder = destFolder

def run(self):

try:

shul.copytree(self.sourceFolder, self.destFolder)

except:

raise

解决方案

The problem is that thread_obj.start() returns immediately. The child thread that you spawned executes in its own context, with its own stack. Any exception that occurs there is in the context of the child thread, and it is in its own stack. One way I can think of right now to communicate this information to the parent thread is by using some sort of message passing, so you might look into that.

Try this on for size:

import sys

import threading

import Queue

class ExcThread(threading.Thread):

def __init__(self, bucket):

threading.Thread.__init__(self)

self.bucket = bucket

def run(self):

try:

raise Exception('An error occured here.')

except Exception:

self.bucket.put(sys.exc_info())

def main():

bucket = Queue.Queue()

thread_obj = ExcThread(bucket)

thread_obj.start()

while True:

try:

exc = bucket.get(block=False)

except Queue.Empty:

pass

else:

exc_type, exc_obj, exc_trace = exc

# deal with the exception

print exc_type, exc_obj

print exc_trace

thread_obj.join(0.1)

if thread_obj.isAlive():

continue

else:

break

if __name__ == '__main__':

main()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值