threading模組,建立執行緒:有3中方法可以用來建立執行緒,推薦採用第3種。
建立乙個 thread 的例項,傳給它乙個函式
建立乙個 thread 的例項,傳給它乙個可呼叫的類物件
從 thread 派生出乙個子類,建立乙個這個子類的例項
方法一:
#!/usr/bin/env python
import threading
from time import sleep, ctime
loops = [ 4, 2 ]
def loop(nloop, nsec):
print 'start loop', nloop, 'at:', ctime()
sleep(nsec)
print 'loop', nloop, 'done at:', ctime()
def main():
print 'starting at:', ctime()
threads =
nloops = range(len(loops))
for i in nloops:
t = threading.thread(target=loop,
args=(i, loops[i]))
for i in nloops: # start threads
threads[i].start()
for i in nloops: # wait for all
threads[i].join() # threads to finish
print 'all done at:', ctime()
if __name__ == '__main__':
main()
方法二:
#!/usr/bin/env python
import threading
from time import sleep, ctime
loops = [ 4, 2 ]
class threadfunc(object):
def __init__(self, func, args, name=''):
self.name = name
self.func = func
self.args = args
def __call__(self):
def loop(nloop, nsec):
print 'start loop', nloop, 'at:', ctime()
sleep(nsec)
print 'loop', nloop, 'done at:', ctime()
def main():
print 'starting at:', ctime()
threads =
nloops = range(len(loops))
for i in nloops: # create all threads
t = threading.thread(
target=threadfunc(loop, (i, loops[i]),
loop.__name__))
for i in nloops: # start all threads
threads[i].start()
for i in nloops: # wait for completion
threads[i].join()
print 'all done at:', ctime()
if __name__ == '__main__':
main()
方法3:
#!/usr/bin/env python
import threading
from time import sleep, ctime
loops = [ 4, 2 ]
class mythread(threading.thread):
def __init__(self, func, args, name=''):
threading.thread.__init__(self)
self.name = name
self.func = func
self.args = args
def run(self):
def loop(nloop, nsec):
print 'start loop', nloop, 'at:', ctime()
sleep(nsec)
print 'loop', nloop, 'done at:', ctime()
def main():
print 'starting at:', ctime()
threads =
nloops = range(len(loops))
for i in nloops:
t = mythread(loop, (i, loops[i]),
loop.__name__)
for i in nloops:
threads[i].start()
for i in nloops:
threads[i].join()
print 'all done at:', ctime()
if __name__ == '__main__':
main()
生產者和消費者
#!/usr/bin/env python
import threading
from time import time, ctime
class mythread(threading.thread):
def __init__(self, func, args, name=''):
threading.thread.__init__(self)
self.name = name
self.func = func
self.args = args
def getresult(self):
return self.res
def run(self):
print 'starting', self.name, 'at:', \
ctime()
print self.name, 'finished at:', \
ctime()
#!/usr/bin/env python
from random import randint
from time import sleep
from queue import queue
from mythread import mythread
def writeq(queue):
print 'producing object for q...',
queue.put('***', 1)
print "size now", queue.qsize()
def readq(queue):
val = queue.get(1)
print 'consumed object from q... size now', \
queue.qsize()
def writer(queue, loops):
for i in range(loops):
writeq(queue)
sleep(randint(1, 3))
def reader(queue, loops):
for i in range(loops):
readq(queue)
sleep(randint(2, 5))
funcs = [writer, reader]
nfuncs = range(len(funcs))
def main():
nloops = randint(2, 5)
q = queue(32)
threads =
for i in nfuncs:
t = mythread(funcs[i], (q, nloops), \
funcs[i].__name__)
for i in nfuncs:
threads[i].start()
for i in nfuncs:
threads[i].join()
print 'all done'
if __name__ == '__main__':
main()
Python學習筆記 多執行緒
引入多執行緒模組 import threading 任何乙個程序啟動的時候會預設的啟動乙個執行緒,這個執行緒叫主線程,主線程可以啟動新的子執行緒 獲取主當前執行緒的名稱 threading.current thread name 建立子執行緒 t threading.thread target fu...
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...
python學習筆記 多執行緒
電腦同時執行有多個軟體,每乙個執行的軟體程式都是乙個程序。每乙個應用程式內,有多段程式同時執行,每一段程式就是乙個執行緒。python3 執行緒中常用的兩個模組為 thread 模組已被廢棄。使用者可以使用 threading 模組代替。所以,在 python3 中不能再使用 thread 模組。為...