1.執行緒建立
1.1函式建立執行緒
def helloworld():
time.sleep(2)
print("hello world")
t = threading.thread(target=helloworld)
t.start()
print("main thread")
1.2類建立執行緒
class helloworld(threading.thread):
def __init__(self,id):
self.id = id
super(helloworld,self).__init__()
def run(self):
time.sleep(2)
print("thread %d helloworld" % self.id)
for i in range(5):
t = helloworld(i)
t.start()
print("main thread")
2.執行緒的方法
t.start() 啟動執行緒活動
t.join() 保證當前執行緒執行完成以後,再執行其他執行緒。
import threading,time
count = 0
def adder():
global count
count = count + 1
time.sleep(0.1)
count = count + 1
threads =
for i in range(100):
thread = threading.thread(target=adder)
thread.start()
for thread in threads:
thread.join()
print(count)
3.執行緒鎖
函式加鎖
import threading
import time
count = 0
def adder(addlock):
global count
addlock.acquire()
count = count +1
addlock.release()
time.sleep(0.1)
addlock.acquire()
count = count + 1
addlock.release()
addlock = threading.lock()
threads =
for i in range(100):
thread = threading.thread(target=adder,args = (addlock,))
thread.start()
for thread in threads:
thread.join()
print(count)
函式with加鎖
import threading
import time
count = 0
def adder(addlock):
global count
with addlock:
count = count +1
time.sleep(0.1)
with addlock:
count = count + 1
addlock = threading.lock()
threads =
for i in range(100):
thread = threading.thread(target=adder,args = (addlock,))
thread.start()
for thread in threads:
thread.join()
print(count)
類wtih加鎖:
import threading
import time
class helloworld(threading.thread):
count =0
addlock = threading.lock()
def run(self):
with helloworld.addlock:
helloworld.count +=1
time.sleep(0.5)
with helloworld.addlock:
helloworld.count += 1
threads =
for i in range(5):
t = helloworld()
t.start()
for t in threads:
t.join()
print(helloworld.count)
python 訊號量
同時訪問5個執行緒。
import time
import threading
semaphore = threading.semaphore(5) #新增乙個計數器
def foo():
semaphore.acquire() #計數器獲得鎖
time.sleep(2) #程式休眠2秒
print("ok",time.ctime())
semaphore.release() #計數器釋放鎖
for i in range(20):
t1=threading.thread(target=foo,args=()) #建立執行緒
t1.start() #啟動執行緒
佇列queue多應用在多執行緒應用中,多執行緒訪問共享變數。對多執行緒而言,訪問共享變數時,佇列queue是執行緒安全的。
import threading
import queue
import time
numconsumers = 2
numprofucers = 2
nummessages = 4
lock = threading.lock()
dataqueue = queue.queue()
def producer(idnum):
for msgnum in range(nummessages):
dataqueue.put("producer id=%d,count=%d" % (idnum,msgnum))
def consumer(idnum):
while not dataqueue.empty():
data = dataqueue.get(block=false)
with lock:
print("consumer",idnum,"got =>",data)
time.sleep(0.1)
if __name__ == '__main__':
consumerthreads =
producerthreads =
for i in range(numprofucers):
t = threading.thread(target = producer,args = (i,))
t.start()
print(dataqueue.qsize())
for i in range(numconsumers):
t = threading.thread(target=consumer,args =(i,))
t.start()
for t in producerthreads:
t.join()
for t in consumerthreads:
t.join()
python多執行緒 不合適cpu密集操作型的任務,適合io操作密集型任務。 Python學習筆記 多執行緒
引入多執行緒模組 import threading 任何乙個程序啟動的時候會預設的啟動乙個執行緒,這個執行緒叫主線程,主線程可以啟動新的子執行緒 獲取主當前執行緒的名稱 threading.current thread name 建立子執行緒 t threading.thread target fu...
Python 學習筆記 執行緒池
前面我們學校裡如何建立多執行緒,當我們接到乙個新的請求時,會建立乙個執行緒,執行完畢之後又銷毀掉這個執行緒。對於一些數目巨大,但是單個快速執行的任務,每個任務真正執行消耗的時間和執行緒建立銷毀的時間可能都差不多。這樣一來,執行緒的效率浪費的比較嚴重。因此可以考慮使用執行緒池的技術,預先建立一些空閒的...
Python學習筆記 多執行緒
簡單的多執行緒 import threading import time def run n print task n time.sleep 2 t1 threading.thread target run,args t1 t2 threading.thread target run,args t2...