线程库--threading
from log import * import threading import time # 共享资源 counter = 0 lock = threading.Lock() def increment(): global counter lock.acquire() try: counter += 1 info("CUrrent thread: {}, Counter: {}".format(threading.current_thread().name, counter)) finally: # 释放锁 lock.release() # 创建10个线程 threads = [] for _ in range(10): t = threading.Thread(target=increment) threads.append(t) t.start() # 等待所有线程完成 for t in threads: t.join()