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
| import random import threading import time
def run_async(func): def wrapper(*args, **kwargs): thr = threading.Thread(target = func, args = args, kwargs = kwargs) thr.start() thr.setName("func-{}".format(func.__name__)) print("线程id={},\n线程名称={},\n正在执行的线程列表:{},\n正在执行的线程数量={},\n当前激活线程={}".format( thr.ident,thr.getName(),threading.enumerate(),threading.active_count(),thr.isAlive) ) return wrapper
@run_async def test(param): while True: time.sleep(3) t = random.randint(1,param) print("/test:" + str(t))
@run_async def test2(param): while True: time.sleep(2) t = random.randint(1,param) print("/test2:" + str(t))
|