我想让Waf在完成执行超过10秒的任何命令时生成一声蜂鸣声。
我不知道如何添加这个并确保代码在Waf退出时执行。
这应该适用于任何Waf命令,而不仅仅是构建。
我检查了Waf书,但我无法找到任何关于我该怎么做的说明。
I want to make Waf generate a beep when it finishes the execution of any command that took more than 10 seconds.
I don't know how do add this and assure that the code executes when Waf exits.
This should run for any Waf command not only build.
I checked the Waf book but I wasn't able to find any indication about how should I do this.
最满意答案
在wscript模块中,您可以使用Python标准库的atexit来注册在进程退出时要调用的callable。 例如:
import atexit import time class MayBeep(object): def __init__(self, deadline=10.0): self.deadline = time.time() + deadline def __call__(self): if time.time() > self.deadline(): print '\7' atexit.register(MayBeep()) ... rest of your wscript module ...当然你可以使用比print '\7'更好的东西用于蜂鸣目的(一直到成熟的多媒体盛会,取决于你导入和使用的其他Python扩展),但这段代码回答了Q的标题 - “添加始终在退出时执行的代码“。
In your wscript module, you can use the Python standard library's atexit to register callables that you want to be called when the process exit. For example:
import atexit import time class MayBeep(object): def __init__(self, deadline=10.0): self.deadline = time.time() + deadline def __call__(self): if time.time() > self.deadline(): print '\7' atexit.register(MayBeep()) ... rest of your wscript module ...Of course you may use something better than print '\7' for beeping purposes (all the way to full-fledged multimedia extravaganzas, depending on what other Python extensions you import and use), but this code answers the Q's title -- "add code that's always executed on exit".
更多推荐
发布评论