如何使用Python装饰器装饰函数

开发 后端
对于Python的GIL和线程安全很多人不是很了解,通过本文,希望能让大家对Python的GIL等内容有所帮助。本文还将就主要谈下笔者对线程安全的一些理解。

经过长时间学习Python装饰器,于是和大家分享一下,看完本文你肯定有不少收获,希望本文能教会你更多东西,学习Python装饰器时,你可能会遇到Python装饰器问题,这里将介绍Python装饰器问题的解决方法,在这里拿出来和大家分享一下。

***个函数deco是装饰函数,它的参数就是被装饰的函数对象。我们可以在deco函数内对传入的函数对象做一番“装饰”,然后返回这个对象(记住一定要返回 ,不然外面调用foo的地方将会无函数可用。

我写了个小例子,检查函数有没有说明文档:、

static PyObject* thread_PyThread_start_new_thread(PyObject *self, PyObject  
 
  *fargs)  
 
{  
 
    PyObject *func, *args, *keyw = NULL;  
 
    struct bootstate *boot;  
 
    long ident;  
 
    PyArg_UnpackTuple(fargs, "start_new_thread", 2, 3, &func, &args, &keyw);  
 
    //[1]:创建bootstate结构  
 
    boot = PyMem_NEW(struct bootstate, 1);  
 
    boot->interp = PyThreadState_GET()->interp;  
 
    boot->funcfunc = func;  
 
    boot->argsargs = args;  
 
    boot->keywkeyw = keyw;  
 
    //[2]:初始化多线程环境  
 
    PyEval_InitThreads(); /* Start the interpreter's thread-awareness */  
 
    //[3]:创建线程  
 
    ident = PyThread_start_new_thread(t_bootstrap, (void*) boot);  
 
    return PyInt_FromLong(ident);  
 
[thread.c]  
 
/* Support for runtime thread stack size tuning.  
 
   A value of 0 means using the platform's default stack size  
 
   or the size specified by the THREAD_STACK_SIZE macro. */  
 
static size_t _pythread_stacksize = 0;  
 
[thread_nt.h]  
 
long PyThread_start_new_thread(void (*func)(void *), void *arg)  
 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.

Python装饰器是装饰函数,它的参数是用来加强“加强装饰”的。由于此函数并非被装饰的函数对象,所以在内部必须至少创建一个接受被装饰函数的函数,然后返回这个对象(实际上此时foo=decomaker(arg)(foo))。

这个我还真想不出什么好例子,还是见识少啊,只好借用同步锁的例子了:

def synchronized(lock):     
    """锁同步装饰方法    
    !lock必须实现了acquire和release方法    
    """    
    def sync_with_lock(func):     
        def new_func(*args, **kwargs):     
            lock.acquire()     
            try:     
                return func(*args, **kwargs)     
            finally:     
                lock.release()     
        new_func.func_name = func.func_name     
        new_func.__doc__ = func.__doc__     
        return new_func     
    return sync_with_lock    
@synchronized(__locker)     
def update(data):     
"""更新计划任务"""    
    tasks = self.get_tasks()     
    delete_task = None    
    for task in tasks:     
        if task[PLANTASK.ID] == data[PLANTASK.ID]:     
            tasks.insert(tasks.index(task), data)     
            tasks.remove(task)     
            delete_task = task     
    r, msg = self._refresh(tasks, delete_task)     
    return r, msg, data[PLANTASK.ID]   
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.

调用时还是updae(data),同时还可以将多个装饰器组合 使用:

def synchronized(lock):     
    """锁同步装饰方法    
    !lock必须实现了acquire和release方法    
    """    
    def sync_with_lock(func):     
        def new_func(*args, **kwargs):     
            lock.acquire()     
            try:     
                return func(*args, **kwargs)     
            finally:     
                lock.release()     
        new_func.func_name = func.func_name     
        new_func.__doc__ = func.__doc__     
        return new_func     
    return sync_with_lock    
@synchronized(__locker)     
def update(data):     
"""更新计划任务"""    
    tasks = self.get_tasks()     
    delete_task = None    
    for task in tasks:     
        if task[PLANTASK.ID] == data[PLANTASK.ID]:     
            tasks.insert(tasks.index(task), data)     
            tasks.remove(task)     
            delete_task = task     
    r, msg = self._refresh(tasks, delete_task)     
    return r, msg, data[PLANTASK.ID]   
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.

学后的总是感觉就是:Python装饰器可以让函数轻装上阵,更重要的是将函数的约束放置于接口处,使意图更加明了,同时又不增加调用者的负担,这贴子还是很肤浅的,我一定会回来的 !

【编辑推荐】

  1. 如何使Python嵌入C++应用程序?
  2. 深入探讨Ruby与Python语法比较
  3. Python学习资料介绍分享
  4. Python学习经验谈:版本、IDE选择及编码解 决方案
  5. 浅析Python的GIL和线程安全
责任编辑:chenqingxiang 来源: 博客园
相关推荐

2023-02-07 07:47:52

Python装饰器函数

2022-09-19 23:04:08

Python装饰器语言

2024-11-04 15:30:43

Python装饰器函数

2021-06-01 07:19:58

Python函数装饰器

2022-05-10 09:12:16

TypeScript装饰器

2024-09-12 15:32:35

装饰器Python

2025-01-22 15:58:46

2022-09-14 08:16:48

装饰器模式对象

2016-11-01 09:24:38

Python装饰器

2024-05-24 11:36:28

Python装饰器

2023-12-11 15:51:00

Python装饰器代码

2021-06-14 09:25:20

PythonPython 3.9编程语言

2022-10-21 07:50:35

装饰器Python编程

2024-09-23 09:00:00

装饰器函数代码

2021-04-11 08:21:20

Python@property装饰器

2023-12-13 13:28:16

装饰器模式Python设计模式

2022-09-21 09:04:07

Python装饰器

2009-12-25 18:12:43

WPF装饰器

2021-07-12 10:24:36

Go装饰器代码

2021-05-27 07:12:19

Python函数装饰器
点赞
收藏

51CTO技术栈公众号