自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

如何使用Python裝飾器裝飾函數(shù)

開發(fā) 后端
對(duì)于Python的GIL和線程安全很多人不是很了解,通過本文,希望能讓大家對(duì)Python的GIL等內(nèi)容有所幫助。本文還將就主要談下筆者對(duì)線程安全的一些理解。

經(jīng)過長時(shí)間學(xué)習(xí)Python裝飾器,于是和大家分享一下,看完本文你肯定有不少收獲,希望本文能教會(huì)你更多東西,學(xué)習(xí)Python裝飾器時(shí),你可能會(huì)遇到Python裝飾器問題,這里將介紹Python裝飾器問題的解決方法,在這里拿出來和大家分享一下。

***個(gè)函數(shù)deco是裝飾函數(shù),它的參數(shù)就是被裝飾的函數(shù)對(duì)象。我們可以在deco函數(shù)內(nèi)對(duì)傳入的函數(shù)對(duì)象做一番“裝飾”,然后返回這個(gè)對(duì)象(記住一定要返回 ,不然外面調(diào)用foo的地方將會(huì)無函數(shù)可用。

我寫了個(gè)小例子,檢查函數(shù)有沒有說明文檔:、

  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]:創(chuàng)建bootstate結(jié)構(gòu)  
  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]:初始化多線程環(huán)境  
  28.  
  29.     PyEval_InitThreads(); /* Start the interpreter's thread-awareness */  
  30.  
  31.     //[3]:創(chuàng)建線程  
  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裝飾器是裝飾函數(shù),它的參數(shù)是用來加強(qiáng)“加強(qiáng)裝飾”的。由于此函數(shù)并非被裝飾的函數(shù)對(duì)象,所以在內(nèi)部必須至少創(chuàng)建一個(gè)接受被裝飾函數(shù)的函數(shù),然后返回這個(gè)對(duì)象(實(shí)際上此時(shí)foo=decomaker(arg)(foo))。

這個(gè)我還真想不出什么好例子,還是見識(shí)少啊,只好借用同步鎖的例子了:

  1. def synchronized(lock):     
  2.     """鎖同步裝飾方法    
  3.     !lock必須實(shí)現(xiàn)了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. """更新計(jì)劃任務(wù)"""    
  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]   

調(diào)用時(shí)還是updae(data),同時(shí)還可以將多個(gè)裝飾器組合 使用:

  1. def synchronized(lock):     
  2.     """鎖同步裝飾方法    
  3.     !lock必須實(shí)現(xiàn)了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. """更新計(jì)劃任務(wù)"""    
  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]   

學(xué)后的總是感覺就是:Python裝飾器可以讓函數(shù)輕裝上陣,更重要的是將函數(shù)的約束放置于接口處,使意圖更加明了,同時(shí)又不增加調(diào)用者的負(fù)擔(dān),這貼子還是很膚淺的,我一定會(huì)回來的 !

【編輯推薦】

  1. 如何使Python嵌入C++應(yīng)用程序?
  2. 深入探討Ruby與Python語法比較
  3. Python學(xué)習(xí)資料介紹分享
  4. Python學(xué)習(xí)經(jīng)驗(yàn)談:版本、IDE選擇及編碼解 決方案
  5. 淺析Python的GIL和線程安全
責(zé)任編輯:chenqingxiang 來源: 博客園
相關(guān)推薦

2023-02-07 07:47:52

Python裝飾器函數(shù)

2024-11-04 15:30:43

Python裝飾器函數(shù)

2021-06-01 07:19:58

Python函數(shù)裝飾器

2022-09-19 23:04:08

Python裝飾器語言

2022-09-14 08:16:48

裝飾器模式對(duì)象

2022-05-10 09:12:16

TypeScript裝飾器

2024-09-12 15:32:35

裝飾器Python

2025-01-22 15:58:46

2016-11-01 09:24:38

Python裝飾器

2023-12-11 15:51:00

Python裝飾器代碼

2024-05-24 11:36:28

Python裝飾器

2022-10-21 07:50:35

裝飾器Python編程

2024-09-23 09:00:00

裝飾器函數(shù)代碼

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函數(shù)裝飾器

2023-12-13 13:28:16

裝飾器模式Python設(shè)計(jì)模式

2021-04-11 08:21:20

Python@property裝飾器

2022-09-21 09:04:07

Python裝飾器
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)