A Method of Improving Interface Access Efficiency by Combining Python Decorator with Thread

  • 2021-11-30 00:59:32
  • OfStack

Review the basic usage of decorators

The essence of decorator is closure, which is a grammatical sugar of python


def outer(fun):
    def inner(*args,**kwargs):
        return fun(*args,**kwargs)
    return inner
#  Decorate with decorators 1 The next two functions 
@outer
def num1():
    print('a')
@outer
def num2():
    print('b')
if __name__ == '__main__':
    print(num1.__name__)
    print(num2.__name__)
 The output of the above code is as follows: 
inner
inner
 Characteristics of decorators : Using a custom decorator changes the function name of the decorated function, 1 Like decorators, you don't have to consider this 1 Dotted, but if multiple functions are decorated by two decorators, an error will be reported because the function name 1 Sample 

Solution: Introduce functools. wraps


import functools
def outer(fun):
    @functools.wraps(fun)
    def inner(*args,**kwargs):
        return fun(*args,**kwargs)
    return inner

The output of the above code is as follows:
num1
num2

Application in actual business

Defining a multithreaded decorator


def async_call(fun):
    def wrapper(*args, **kwargs):
        Thread(target=fun, args=args, kwargs=kwargs).start()
    return wrapper

You can add this decorator to interfaces that need to improve efficiency
Because threads normally execute faster than processes

You can use the decorator to test and count the running time of the function


import time
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() - t:.8f} s')
        return result
    return fun

This decorator is interested in friends can be saved, after the test interface performance can be directly used!


from time import sleep
from time import time
import time
from threading import Thread
# This is a decorator for counting time 
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() - t:.8f} s')
        return result
    return fun
# This is the decorator for creating threads , Interested ones can be saved 1 Under, you can use it directly 
def async_call(fun):
    def wrapper(*args, **kwargs):
        Thread(target=fun, args=args, kwargs=kwargs).start()
    return wrapper
@coast_time
@async_call
def hello():
    print('start')
    sleep(2)
    print('end')
    return
if __name__ == "__main__":
    hello()

Run time without creating threads is: 2s more
Time to use thread decorator: 0.0003 s

functools. wraps can be introduced to prevent function names from being changed when decorating multiple functions

The above is the Python decorator and thread combination to improve the efficiency of interface access methods in detail, more about Python to improve the efficiency of interface access information please pay attention to other related articles on this site!


Related articles: