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

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

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

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

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

  1. static PyObject* thread_PyThread_start_new_thread(PyObject *self, PyObject  
  2.  
  3.   *fargs)  
  4.  
  5. {  
  6.  
  7.     PyObject *func, *args, *keyw = NULL;  
  8.  
  9.     struct bootstate *boot;  
  10.  
  11.     long ident;  
  12.  
  13.     PyArg_UnpackTuple(fargs, "start_new_thread", 2, 3, &func, &args, &keyw);  
  14.  
  15.     //[1]:创建bootstate结构  
  16.  
  17.     boot = PyMem_NEW(struct bootstate, 1);  
  18.  
  19.     boot->interp = PyThreadState_GET()->interp;  
  20.  
  21.     boot->funcfunc = func;  
  22.  
  23.     boot->argsargs = args;  
  24.  
  25.     boot->keywkeyw = keyw;  
  26.  
  27.     //[2]:初始化多线程环境  
  28.  
  29.     PyEval_InitThreads(); /* Start the interpreter's thread-awareness */  
  30.  
  31.     //[3]:创建线程  
  32.  
  33.     ident = PyThread_start_new_thread(t_bootstrap, (void*) boot);  
  34.  
  35.     return PyInt_FromLong(ident);  
  36.  
  37. [thread.c]  
  38.  
  39. /* Support for runtime thread stack size tuning.  
  40.  
  41.    A value of 0 means using the platform's default stack size  
  42.  
  43.    or the size specified by the THREAD_STACK_SIZE macro. */  
  44.  
  45. static size_t _pythread_stacksize = 0;  
  46.  
  47. [thread_nt.h]  
  48.  
  49. long PyThread_start_new_thread(void (*func)(void *), void *arg)  
  50.  

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

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

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

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

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

学后的总是感觉就是: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装饰器语言

2021-06-01 07:19:58

Python函数装饰器

2022-09-14 08:16:48

装饰器模式对象

2022-05-10 09:12:16

TypeScript装饰器

2016-11-01 09:24:38

Python装饰器

2023-12-11 15:51:00

Python装饰器代码

2022-10-21 07:50:35

装饰器Python编程

2021-06-14 09:25:20

PythonPython 3.9编程语言

2009-12-25 18:12:43

WPF装饰器

2024-02-26 00:00:00

TypeScript装饰器decorators

2021-05-27 07:12:19

Python函数装饰器

2022-09-21 09:04:07

Python装饰器

2021-04-11 08:21:20

Python@property装饰器

2023-12-13 13:28:16

装饰器模式Python设计模式

2021-07-12 10:24:36

Go装饰器代码

2023-08-07 16:07:42

2015-06-26 11:33:23

Python装饰器使用场景实践

2022-09-27 11:01:08

Python装饰器

2021-07-27 15:58:12

Python日志代码
点赞
收藏

51CTO技术栈公众号