开发者

Class level return/exit in Python

In my application I have a loop that in each turn creates an instance of a class which performs some operations. In this class I have a signal timer that when invoked is supposed to exit the curr开发者_高级运维ent turn in the loop gracefully and allow the next operation to continue without any interruption.

In it's current state it raises an exception that is supposed to be caught by a try-except-block. However I have a "loadFinished" signal (that I don't know the id for) that is invoked after the exception is thrown and causes a runtime exception on the function definition that I can't catch.

Is there a way to do a class level return like one would do to end a function if something is wrong? Or maybe some kind of sys.exit() that only works on the class?

I hope I made myself clear. :) Thank you for your help.


Christopher,

maybe using Timer and Event classes is more appropriate for you. You could check for the event to be set inside you loop an then exit the current iteration:

if my_event.isSet():
    continue

I think what you are trying to do is not really a good design. I see no way to "throw" an exception into a different function. I assume by signal you mean asynchronous function call. This of course could also set the event mentioned above.


If you're talking about signals as in the signal module, then the handlers aren't called in a standard fashion.

You can't throw an exception out of them and expect it to appear in your normal code. Indeed you should try to do as little as possible in your handler - set a global flag, or send a message on a pipe perhaps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜