如何在退出之前添加总是由Waf执行的代码?(How can I add a code that is always executed by Waf before exit?)

编程入门 行业动态 更新时间:2024-10-26 20:21:28
如何在退出之前添加总是由Waf执行的代码?(How can I add a code that is always executed by Waf before exit?)

我想让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".

更多推荐

本文发布于:2023-04-29 11:33:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1336424.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:是由   代码   如何在   executed   exit

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!