如何从其他线程获得引发的异常 [英] How to get a raised exception from other thread

查看:64
本文介绍了如何从其他线程获得引发的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


如何从

导入模块中的其他线程中获取引发异常?

例如:


---------------

programA.py

---------------


导入程序B


thread = programB.MakeThread ()

thread.start()


---------------

programB.py

---------------

导入线程,时间


class SomeException(Exception):

pass


class MakeThread(threading.Thread):

def __init __(self):

threading.Thread .__ init __(self)


def run(self):

i = 0

而1:

打印我

i + = 1

time.sleep(1)#等待一秒继续

if i> 10:

提高SomeException()

谢谢


Daniel

解决方案

在非Windows系统上有很多方法可以做到这一点 - 这几乎是一个完整的领域。 :)(D-BUS,fifos,socket,shmfs等)在

Windows中,我不会有任何线索。


我猜这是一个很难回答的问题,而不需要更多的信息。 :)


星期五,2005-10-14在14:45 -0700,dcrespo写道:

大家好,
如何从
导入模块中的其他线程获得引发异常?

例如:

--------- ------
programA.py
---------------

导入程序B

thread = programB.MakeThread()
thread.start()

---------------
programB.py
---------------
导入线程,时间

类SomeException(例外):
传递

class MakeThread(threading.Thread):
def __init __(self):
threading.Thread .__ init __(self)

def run(self):
i = 0
1:
打印i
i + = 1
time.sleep(1)#等待一秒钟继续
如果我> 10:提高SomeException()

谢谢

Daniel




感谢您的回答,但我认为在python中创建的线程

应该在windows和linux上创建一个线程。


你能告诉我Python如何做我想做的事吗?谢谢


dcrespo写道:

如何从
导入的其他线程中获取引发的异常模块?


定义什么是获取用于您的目的。看来你的意思是

想要捕获异常,但是在首先启动另一个

线程的线程中。如果这是真的,请显示你想要捕获这个异常的地方你想要捕获这个异常,因为当你启动线程时,

主线程继续运行甚至可能完成另一个之前

线程结束。

thread = programB.MakeThread()
thread.start()



....

#稍后代码可能在线程结束时完成

....


您是否正在寻找某种类型的

thread.waitUntilCompletionAndReRaiseExceptions()方法?


-Peter


Hi all,

How can I get a raised exception from other thread that is in an
imported module?

For example:

---------------
programA.py
---------------

import programB

thread = programB.MakeThread()
thread.start()

---------------
programB.py
---------------
import threading, time

class SomeException(Exception):
pass

class MakeThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)

def run(self):
i = 0
while 1:
print i
i += 1
time.sleep(1) #wait a second to continue
if i>10:
raise SomeException()
Thanks

Daniel

解决方案

On non-Windows system there are a ton of ways to do it--this is almost a
whole field unto itself. :) (D-BUS, fifos, sockets, shmfs, etc.) In
Windows, I wouldn''t have a clue.

I guess this is a hard question to answer without a bit more
information. :)

On Fri, 2005-10-14 at 14:45 -0700, dcrespo wrote:

Hi all,

How can I get a raised exception from other thread that is in an
imported module?

For example:

---------------
programA.py
---------------

import programB

thread = programB.MakeThread()
thread.start()

---------------
programB.py
---------------
import threading, time

class SomeException(Exception):
pass

class MakeThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)

def run(self):
i = 0
while 1:
print i
i += 1
time.sleep(1) #wait a second to continue
if i>10:
raise SomeException()
Thanks

Daniel




Thanks for your answer, but I think that created thread in python
should create a thread either on windows and linux.

Can you give me Python example of how to do what I want to do? Thanks


dcrespo wrote:

How can I get a raised exception from other thread that is in an
imported module?
Define what "get" means for your purposes. It appears that you mean you
want to catch the exception, but in the thread which launched the other
thread in the first place. If that''s true, please show where you would
expect to catch this exception, given that when you start the thread,
the main thread continues running and might even finish before the other
thread finishes.
thread = programB.MakeThread()
thread.start()


....
# later code which might have completed by the time the thread finishes
....

Are you looking, for example, for some kind of
thread.waitUntilCompletionAndReRaiseExceptions() method?

-Peter


这篇关于如何从其他线程获得引发的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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