首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用atexit执行结构清理操作

使用atexit执行结构清理操作
EN

Stack Overflow用户
提问于 2013-07-15 00:18:57
回答 1查看 527关注 0票数 2

关于如何清理(例如,删除临时文件等),是否有公认的智慧?在fabric任务中?如果我像往常一样使用atexit模块,那么就会遇到困难,因为我不能使用@roles装饰器来修饰传递给atexit.register()的函数。或者我可以吗?其他fabric用户是如何处理这一问题的?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-07-19 01:40:38

我也有同样的问题。Next Code并不理想,但我目前有一个类似这样的实现。

fabfile.py

代码语言:javascript
复制
from functools import wraps
from fabric.network import needs_host
from fabric.api import run, env

def runs_final(func):
    @wraps(func)
    def decorated(*args, **kwargs):
        if env.host_string == env.all_hosts[-1]:
            return func(*args, **kwargs)
        else:
            return None
    return decorated

@needs_host
def hello():
    run('hostname')
    atexit()

@runs_final
def atexit():
    print ('this is at exit command.')

结果:

代码语言:javascript
复制
fabric$ fab hello -H web01,web02
>[web01] Executing task 'hello'
>[web01] run: hostname
>[web01] out: web01
>[web01] out: 
>[web02] Executing task 'hello'
>[web02] run: hostname
>[web02] out: web02
>[web02] out: 
>
>this is at exit command.
>
>Done.
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17641195

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档