开发者

Interrupt method execution after arbitrary time in Python

开发者 https://www.devze.com 2022-12-20 09:52 出处:网络
I have some method开发者_如何学JAVA doA() that occasionally hangs for a while. Are there any common modules in Python that can control time of doA() execution and interrupt it?

I have some method开发者_如何学JAVA doA() that occasionally hangs for a while. Are there any common modules in Python that can control time of doA() execution and interrupt it? Of course, it may be implemented via threads, so simple wrapper around threading module may be good solution.

In other words i'd like to have code like:

import CoolAsyncControl
CoolAsyncControl.call(self.doA, timeout = 10)


You can use a threading.Timer to call thread.interrupt_main, as long as you're running doA in the main thread. Note that the syntax you desire is impossible because Python (like most languages, excepting e.g. Haskell) is "eager" -- arguments are entirely computed before a call is performed, so the self.doA() would run to completion before the call method has any chance to do anything about it! You'll have to use some syntax such as

CoolIt.call(self.doA, args=(), timeout=100)

so that call can set the timer up before performing the call to doA with the given arguments.

0

精彩评论

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

关注公众号