執行緒基礎:匯入,建立函式,建立線和執行
importthread
import
time
#為執行緒定義乙個函式
defprint_time(threadname, delay):
count =0
while count < 5:
time.sleep(delay)
count += 1
"%s: %s
" %(threadname, time.ctime(time.time()))
#建立兩個執行緒
try:
thread.start_new_thread(print_time, (
"thread-1
", 2,)) #
函式名和它的兩個引數
thread.start_new_thread(print_time, ("
thread-2
", 5,))
except
:
"error: unable to start thread
"while 1:
pass
#兩個執行緒會同時分別進行,乙個每五秒列印一次,乙個每2秒列印一次
importthreading
import
time
exitflag =0
class mythread(threading.thread): #
繼承父類 threading.thread
def__init__
(self, threadid, name, counter):
threading.thread.
__init__
(self)
self.threadid =threadid
self.name =name
self.counter =counter
def run(self): #
把要執行的**寫到 run 函式裡面 執行緒在建立後會直接執行 run 函式,所以基本可以理解為threading中,function run裡的函式會自動執行
"starting
" +self.name
print_time(self.name, self.counter, 5)
"exiting
" +self.name
def print_time(threadname, delay, counter): #
這個是單獨的乙個函式
while
counter:
ifexitflag:
(threading.thread).exit()
time.sleep(delay)
"%s: %s
" %(threadname, time.ctime(time.time()))
counter -= 1
#建立新執行緒
thread1 = mythread(1, "
thread-1
", 1)
thread2 = mythread(2, "
thread-2
", 2)
#開啟執行緒
thread1.start()
thread2.start()
"exiting main thread
"
importthreading
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):
"starting
" +self.name
#上鎖threadlock.acquire()
print_time(self.name, self.counter, 3)
#釋放鎖
threadlock.release()
defprint_time(threadname, delay, counter):
while
counter:
time.sleep(delay)
"%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()
"exiting main thread
"
4,多個執行緒之間的同步用佇列
importqueue
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):
"starting
" +self.name
process_data(self.name, self.q)
"exiting
" +self.name
defprocess_data(threadname, q): #實際上就是單個執行緒輪換著對佇列進行某個操作,
while
notexitflag:
queuelock.acquire()
ifnot
workqueue.empty():
data =q.get()
queuelock.release()
"%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迴圈建立新執行緒
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()
"exiting main thread
"#實際上就是開三個執行緒清空了乙個佇列,執行緒輪流參與,實現執行緒之間的同步
python2執行緒 python多執行緒2執行緒應用
上 1 經典的生產者,消費者問題 2 lock和rlock差不多 lock會死鎖,rlock不會,具體google coding gbk created on 2013 1 4 author jimmy note 1 乙個簡單的建立執行緒例子,外加生產者消費者問題 2 執行緒同步初步 import ...
玩轉python(2)多執行緒的歷史2
執行緒這個概念早在多核cpu出現之前就提出來了,單核時代的多執行緒主要是為了讓cpu盡量不處於空閒狀態,使其計算能力始終能得到利用。但本質上講,在任意時刻只有乙個執行緒在執行。儘管任意時刻只有乙個執行緒在執行,但是依然有些問題需要解決,其中最重要的就是執行緒安全。這個問題的 很簡單,我之前說過,cp...
Python 2 基礎中的基礎知識
我選擇最新版python直譯器版本 從最簡單的hello world 開始 print hello world hello world 就像最簡單的計算器 不用多說,一看就懂 2 6 8 2 6 0.3333333333333333 2 6 0 5 9 45 5.0 6 0.833333333333...