Python多執行緒

2022-02-16 08:51:28 字數 4327 閱讀 6525

#

!/usr/bin/env python3

#coding=utf-8

import

threading

import

time

import

threadtime

exitflag=0

class

mythread(threading.thread):

def__init__

(self,threadid,name,counter):

threading.thread.

__init__

(self)

self.threadid=threadid

self.name=name

self.counter=counter

defrun(self):

print("

開始執行緒:

"+self.name)

print_time(self.name,self.counter,5)

print("

退出執行緒:

"+self.name)

defprint_time(threadname,delay,counter):

while

counter:

ifexitflag:

threadname.exit()

time.sleep(delay)

print("

%s:%s

"%(threadname,time.ctime(time.time())))

counter-=1

#建立連個執行緒

try:

#建立新執行緒

thread1=mythread(1,"

thread-1

",1)

thread2=mythread(2,"

thread-2

",2)

#開啟新執行緒

thread1.start()

thread2.start()

thread1.join()

thread2.join()

print("

退出主線程")

except

:

print("

error:無法啟動執行緒

")

#執行緒同步

#!/usr/bin/env python3

#coding=utf-8

import

threading

import

time

class

mythread(threading.thread):

def__init__

(self,threadid,name,counter):

threading.thread.

__init__

(self)

self.threadid=threadid

self.name=name

self.counter=counter

defrun(self):

print("

開啟執行緒:

"+self.name)

#獲取鎖,用於執行緒同步

threadlock.acquire()

print_time(self.name,self.counter,6)

#釋放鎖,開啟下乙個執行緒

threadlock.release()

defprint_time(threadname,delay,counter):

while

counter:

time.sleep(delay)

print("

%s:%s

"%(threadname,time.ctime(time.time())))

counter-=1threadlock=threading.lock()

threads=

#建立新執行緒

thread1=mythread(1,"

thread-1

",1)

thread2=mythread(2,"

thread-2

",2)

#開啟新執行緒

thread1.start()

thread2.start()

#新增執行緒到執行緒列表

#等待所有執行緒完成

for t in

threads:

t.join()

print("

退出執行緒

"+t.name)

print("

退出主線程

")

#

!/usr/bin/env python3

#coding=utf-8

#佇列 安全

import

queue

import

threading

import

time

exitflag =0

class

mythread (threading.thread):

def__init__

(self, threadid, name, q):

threading.thread.

__init__

(self)

self.threadid =threadid

self.name =name

self.q =q

defrun(self):

print ("

開啟執行緒:

" +self.name)

process_data(self.name, self.q)

print ("

退出執行緒:

" +self.name)

defprocess_data(threadname, q):

while

notexitflag:

queuelock.acquire()

ifnot

workqueue.empty():

data =q.get()

queuelock.release()

print ("

%s processing %s

" %(threadname, data))

else

: queuelock.release()

time.sleep(1)

threadlist = ["

thread-1

", "

thread-2

", "

thread-3"]

namelist = ["

one", "

two", "

three

", "

four

", "

five"]

queuelock =threading.lock()

workqueue = queue.queue(10)

threads =

threadid = 1

#建立新執行緒

for tname in

threadlist:

thread =mythread(threadid, tname, workqueue)

thread.start()

threadid += 1

#填充佇列

queuelock.acquire()

for word in

namelist:

workqueue.put(word)

queuelock.release()

#等待佇列清空

while

notworkqueue.empty():

pass

#通知執行緒是時候退出

exitflag = 1

#等待所有執行緒完成

for t in

threads:

t.join()

print ("

退出主線程

")執行結果:

開啟執行緒:thread-2

開啟執行緒:thread-3

thread-1 processing one

thread-3 processing two

thread-2 processing three

thread-1 processing four

thread-3 processing five

退出執行緒:thread-2

退出執行緒:thread-1

退出執行緒:thread-3

退出主線程

python多執行緒 python多執行緒

通常來說,多程序適用於計算密集型任務,多執行緒適用於io密集型任務,如網路爬蟲。關於多執行緒和多程序的區別,請參考這個 下面將使用python標準庫的multiprocessing包來嘗試多執行緒的操作,在python中呼叫多執行緒要使用multiprocessing.dummy,如果是多程序則去掉...

python多執行緒詳解 Python多執行緒詳解

前言 由於最近的工作中一直需要用到python去處理資料,而在面對大量的資料時,python多執行緒的優勢就展現出來了。因而藉此機會,盡可能詳盡地來闡述python多執行緒。但對於其更底層的實現機制,在此不做深究,僅是對於之前的一知半解做個補充,也希望初學者能夠通過這篇文章,即便是照葫蘆畫瓢,也能夠...

python程式多執行緒 PYTHON多執行緒

在單執行緒的情況下,程式是逐條指令順序執行的。同一時間只做乙個任務,完成了乙個任務再進行下乙個任務。比如有5個人吃飯,單執行緒一次只允許乙個人吃,乙個人吃完了另乙個人才能接著吃,假如每個人吃飯都需要1分鐘,5個人就需要5分鐘。多執行緒的情況下,程式就會同時進行多個任務,雖然在同一時刻也只能執行某個任...