下面代码是《python核心编程》关于多线程编程一章中的一个例子:
#!/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):
apply(self.func, self.args)
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__)
threads.append(t)
for i in nloops:
threads[i].start()
for i in nloops:
threads[i].join()
print 'all DONE at:', ctime()
if __name__ == '__main__':
main()
书上显示的输出结果是这样的
我自己打了一遍,输出结果是这样的
可以看到,我的loop0和loop1的显示内容混合到一起了,这样是对的吗?为什么会这样?
巴扎黑2017-04-18 09:19:00
這裡需要加鎖,標準輸出是共享資源,大家都可以同時向螢幕寫東西,所以可能混亂。
這裡需要加入互斥鎖,告訴別的線程,我現在要寫,你們先別寫,然後寫完了告訴別的線程,我寫完了,你們可以申請寫了。
loop 函數寫成:
import threading
#创建锁
mutex = threading.Lock()
def loop(nloop, nsec):
#锁定
mutex.acquire()
print 'start loop', nloop, 'at:', ctime()
sleep(nsec)
print 'loop', nloop, 'done at:', ctime()
#释放
mutex.release()
所有代碼為:
#!/usr/bin/env python
# encoding: utf-8
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):
apply(self.func, self.args)
# 创建锁
mutex = threading.Lock()
def loop(nloop, nsec):
# 锁定
mutex.acquire()
print 'start loop', nloop, 'at:', ctime()
sleep(nsec)
print 'loop', nloop, 'done at:', ctime()
# 释放
mutex.release()
def main():
print 'starting at:', ctime()
threads = []
nloops = range(len(loops))
for i in nloops:
t = MyThread(loop, (i, loops[i]),
loop.__name__)
threads.append(t)
for i in nloops:
threads[i].start()
for i in nloops:
threads[i].join()
print 'all DONE at:', ctime()
if __name__ == '__main__':
main()
PHPz2017-04-18 09:19:00
這裡涉及3個知識點
python 線程調度策略
python的線程實際上就是作業系統所支援的原生線程,python的多線程機制建立在作業系統的原生線程機制之上,不同的作業系統有不同的實作
window/linux python 執行緒調度策略
搜尋 “window 執行緒調度策略” or "linux 執行緒調度策略"
python GIL 鎖 —— PYTHON單一進程無並行
然後你就懂了