How to improve the efficiency of interface access by combining Python decorator and thread
This article mainly introduces the Python decorator and thread combination of how to improve interface access efficiency, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand.
Review the basic usage of decorators
The essence of decorator is closure, which is a kind of grammatical sugar of python.
Def outer (fun): def inner (* args,**kwargs): return fun (* args) * * kwargs) return inner# uses the decorator to decorate two functions @ outerdef num1 (): print ('a') @ outerdef num2 (): print ('b') if _ _ name__ = ='_ main__': print (num1.__name__) print (num2.__name__) the above code output result: the characteristics of the innerinner decorator: using a custom decorator will change the function name of the decorated function General decorators do not have to consider this, but if multiple functions are decorated by two decorators, an error will be reported, because the function name is the same.
Solution: introduce functools.wraps
Import functoolsdef outer (fun): @ functools.wraps (fun) def inner (* args,**kwargs): return fun (* args,**kwargs) return inner
The above code outputs the result:
Num1
Num2
Application in actual business
Define a multithreaded decorator
Def async_call (fun): def wrapper (* args, * * kwargs): Thread (target=fun, args=args, kwargs=kwargs). Start () return wrapper
The decorator can be added to the interface that needs to improve efficiency.
Because normally, threads execute faster than processes.
You can test and count the running time of the function with the decorator
Import timedef coast_time (func): def fun (* args, * * kwargs): t = time.perf_counter () result = func (* args, * * kwargs) print (f'func {func.__name__} coast time: {time.perf_counter ()-tpur.8f} s') return result return fun
If you are interested in this decorator, you can save it and test the performance of the interface in the future.
From timeimport sleepfrom timeimport timeimport timefrom threading import Thread# this is the time statistics decorator def coast_time (func): def fun (* args, * * kwargs): t = time.perf_counter () result = func (* args) * * kwargs) print (f'func {func.__name__} coast time: {time.perf_counter ()-tbank .8f} s') return result return fun# this is a decorator for creating threads. If you are interested, you can save it. Def async_call (fun): def wrapper (* args, * * kwargs): Thread (target=fun, args=args, kwargs=kwargs). Start () return wrapper@coast_time@async_calldef hello (): print ('start') sleep (2) print (' end') returnif _ name__ = = "_ _ main__": hello ()
The running time of not creating a thread is more than 2 seconds.
Time to use thread decorator: 0.0003s
When introducing functools.wraps to prevent decorating multiple functions, the function name can be changed.
Thank you for reading this article carefully. I hope the article "how to improve interface access efficiency with the combination of Python decorator and thread" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!