CLOVER馃崁

That was when it all began.

Python銇偣銉儍銉夈伅銆併儘銈ゃ儐銈c儢銈广儸銉冦儔銇伄銇嬶紵

Python銇с伄銈广儸銉冦儔銇ㄣ亜銇堛伆銆丟IL锛圙lobal Interpreter Lock锛夈亴銇傘倠銇仹鍚屾檪銇伅銇层仺銇ゃ伄銈广儸銉冦儔銇椼亱瀹熻銇с亶銇亜
銇ㄣ亜銇嗚┍銈夈仐銇勩仹銇欍

銈广儸銉冦儔鐘舵厠 (thread state) 銇ㄣ偘銉兗銉愩儷銈ゃ兂銈裤儣銉偪銉儍銈 (global interpreter lock)

動的言語総まとめ : PythonのGILが徹底改善されるも廃止はされない。SqueakがAndroidに移植された。

Pythonのマルチスレッドで気を付けるべきこと | 凡人のブログ

銇ㄣ亾銈嶃仹銆併亾銇偣銉儍銉夈仯銇S銇儘銈ゃ儐銈c儢銈广儸銉冦儔銇伄銇с仐銈囥亞銇嬶紵銇°倗銇c仺纰鸿獚銇椼仸銇裤仧銇忋仾銈娿伨銇椼仧銆

鐠板

纰鸿獚銇椼仧鐠板銇併亾銇°倝銆

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 18.04.2 LTS
Release:    18.04
Codename:   bionic


$ python3 -V
Python 3.6.7

Ubuntu Linux 18.04 LTS銆Python 3.6銇с仚銆

纰鸿獚

銇撱倱銇儣銉偘銉┿儬銈掔敤鎰忋
thread-example.py

import threading
import time

def run():
    current_thread_name = threading.current_thread().getName()
    print("start Thread = %s" % current_thread_name)

    time.sleep(10)

    print("end Thread = %s" % current_thread_name)

print("start!!")

threads = [threading.Thread(target = run, name = "Thread-%d" % (i + 1)) for i in range(5)]

for thread in threads:
    thread.start()

for thread in threads:
    thread.join()

print("finish!!")

銈广儸銉冦儔銈5銇ょ敤鎰忋仐銇﹂枊濮嬨佸悇銈广儸銉冦儔銇10绉掑緟姗熴仐銇︾祩浜嗐仐銇俱仚銆

銇俱仛銇佸疅琛屻

$ time python3 thread-example.py 
start!!
start Thread = Thread-1
start Thread = Thread-2
start Thread = Thread-4
start Thread = Thread-3
start Thread = Thread-5
end Thread = Thread-4
end Thread = Thread-3
end Thread = Thread-2
end Thread = Thread-1
end Thread = Thread-5
finish!!

real    0m10.039s
user    0m0.030s
sys 0m0.000s

10绉掋仹绲傘倧銈娿伨銇椼仧銆

銇с伅銆併亾銇儣銉偘銉┿儬銈掑疅琛屼腑銇

$ time python3 thread-example.py

銈傘亞銇层仺銇ゃ伄銈裤兗銉熴儕銉仹銆併儣銉偦銈广亱銈夋淳鐢熴仐銇熴偣銉儍銉夈倰纰鸿獚銇椼仸銇裤伨銇欍

$ ps aux -L | grep python3 | grep thread-example.py
xxxxx 13748 13748  0.1    6  0.0 407140  9912 pts/0    Sl+  00:24   0:00 python3 thread-example.py
xxxxx 13748 13749  0.0    6  0.0 407140  9912 pts/0    Sl+  00:24   0:00 python3 thread-example.py
xxxxx 13748 13750  0.0    6  0.0 407140  9912 pts/0    Sl+  00:24   0:00 python3 thread-example.py
xxxxx 13748 13751  0.0    6  0.0 407140  9912 pts/0    Sl+  00:24   0:00 python3 thread-example.py
xxxxx 13748 13752  0.0    6  0.0 407140  9912 pts/0    Sl+  00:24   0:00 python3 thread-example.py
xxxxx 13748 13753  0.0    6  0.0 407140  9912 pts/0    Sl+  00:24   0:00 python3 thread-example.py

鍏ㄩ儴銇6銇ゃ亗銈娿伨銇欍伃銆傘伈銇ㄣ仱澶氥亜銇伅銆併儭銈ゃ兂銈广儸銉冦儔銇с仚銇傘仺銇勩亞銈忋亼銇с丱S銉嶃偆銉嗐偅銉栥仾銈广儸銉冦儔銇倛銇嗐仹銇欍

銈姐兗銈广偝銉笺儔銈掋仌銈夈仯銇ㄨ銇︺伩銇俱仐銈囥亞銆

https://github.com/python/cpython/blob/v3.6.7/Lib/threading.py#L32

https://github.com/python/cpython/blob/v3.6.7/Python/thread.c#L84-L87

https://github.com/python/cpython/blob/v3.6.7/Python/thread_pthread.h#L159-L178

POSIX銈广儸銉冦儔銇裤仧銇勩仹銇欍伃銆

Windows銇牬鍚堛伅鈥︼紵

https://github.com/python/cpython/blob/v3.6.7/Python/thread.c#L89-L92

https://github.com/python/cpython/blob/v3.6.7/Python/thread_nt.h#L145-L151

銇ㄣ亾銈嶃仹銆併孏IL銇屻亗銈嬪壊銇伅銈广儶銉笺儣銇欍倠銉椼儹銈般儵銉犮伅10绉掋仹绲傘倧銇c仸銇勩倠銇仹銇紵銆嶃仺鎬濄亞銇嬨倐銇伄銇с仚銇屻
銇撱倢銇偣銉兗銉楁檪銇獹IL銈掕В鏀俱仚銈嬨亱銈夈仹銇椼倗銇嗐伃銇団︺

karky7のブログ: PythonのGILについて簡単に調べてみました

銇°倝銇c仺銈姐兗銈广偝銉笺儔銈掕銇︺伩銈嬨仺銆併仢銇嗐伩銇熴亜銇с仚銇

https://github.com/python/cpython/blob/v3.6.7/Modules/timemodule.c#L1447-L1449

Py_BEGIN_ALLOW_THREADS
... Do some blocking I/O operation ...
Py_END_ALLOW_THREADS

鎷″嫉銈炽兗銉夊唴銇 GIL 銈掕В鏀俱仚銈