Python编程多线程开发:提升效率的麻辣香锅派送与文档打印实战

IT巴士 16 0

import threading

def delivery_food(order):

print(f"正在派送{order}...")

courier = threading.Thread(target=delivery_food, args=("麻辣香锅",)) courier.start() class MyCourier(threading.Thread):

def run(self):
    print("专属骑手已出发")
    

special_courier = MyCourier() special_courier.start()

import threading

printer_lock = threading.Lock() shared_printer = []

def print_document(doc):

with printer_lock:  # 自动获取和释放锁
    shared_printer.append(doc)
    print(f"正在打印: {doc}")

threads = [threading.Thread(target=print_document, args=(f"文档{i}",))

       for i in range(10)]

for t in threads:

t.start()

for t in threads:

t.join()

标签: #Python多线程编程 #threading模块使用 #共享资源管理 #Python锁机制 #多线程实战示例