功能上的属性是否是线程安全的

时间:2018-08-22 16:17:00

标签: python thread-safety

我创建了很多函数。每个功能可能会或可能不会在内部使用其他功能。在原始函数调用的结尾,我想清除它们创建的所有混乱(假设我们清除了tmp文件)。为此,我创建了一个带有计数器的装饰器,执行完最后一个功能后,计数器的计数值降为0,然后执行清除操作。但是这样的解决方案线程/过程/伏都安全吗?

为了更好地描述我在做什么,我在剪下工作后创建了以下内容:

import functools


def control(func):
    @functools.wraps(func)
    def inner(*args, **kwargs):
        try:
            control.x += 1

            print(func.__name__)

            result = func(*args, **kwargs)
        except Exception:
            raise
        finally:
            control.x -= 1

        if control.x == 0:
            ignition()

        return result

    return inner


control.x = 0


@control
def launch():
    check_engine()
    check_control()
    check_seatbelts()


@control
def check_engine():
    print("\t - ALL GREEN")
    check_fuel()


@control
def check_fuel():
    print("\t - ALL GREEN")


@control
def check_control():
    print("\t - ALL GREEN")


@control
def check_seatbelts():
    print("\t - ALL GREEN")


def ignition():
    print("IGNITE!")


launch()

0 个答案:

没有答案
相关问题