[Python_5] Python 线程
0. 说明
Python 线程笔记
1. 低级 API
# -*-coding:utf-8-*- """ 线程 """ """ 低级 API """ import time import _thread def sayHello(str): print(str) try: _thread.start_new_thread(sayHello, ("hello world",)) finally: # 休眠的秒数 time.sleep(5)
2. 高级 API
玩家线程
""" 高级 API """ import threading # 玩家类 class Player(threading.Thread): def run(self): print("%s 出发了" % (self.name)) time.sleep(self.time) print("%s 到了" % (self.name)) def __init__(self, name, time): threading.Thread.__init__(self) self.name = name self.time = time p1 = Player("p1", 3) p2 = Player("p2", 1) p3 = Player("p3", 2) p4 = Player("p4", 4) p1.start() p2.start() p3.start() p4.start() p1.join() p2.join() p3.join() p4.join() print("开局!!")
售票问题解决多线程安全问题
import threading tickets = 100 # 取票方法,保证线程安全性 lock = threading.Lock() def getTicket(): global tickets lock.acquire() tmp = tickets if (tickets > 0): tickets -= 1 lock.release() return tmp else: lock.release() return -1 # 售票员 class Saler(threading.Thread): def run(self): while (True): tmp = getTicket() if tmp != -1: print("%s : %d\r\n" % (self.name, tmp), end="") else: break; def __init__(self, name): threading.Thread.__init__(self) self.name = name s2 = Saler("s2") s1 = Saler("s1") s1.start() s2.start() s1.join() s2.join() print("over")
且将新火试新茶,诗酒趁年华。