W1N*_*Zr0 3 python multithreading factorial blocking gil
为什么math.factorial在一个线程中表现得如此奇怪?
这是一个例子,它创建了三个线程:
它调用start线程,然后join超时
睡眠和旋转线程按预期工作并立即返回start,然后坐在join超时.
另一方面,析取线程start直到它运行到最后才返回!
import sys
from threading import Thread
from time import sleep, time
from math import factorial
# Helper class that stores a start time to compare to
class timed_thread(Thread):
def __init__(self, time_start):
Thread.__init__(self)
self.time_start = time_start
# Thread that just executes sleep()
class sleep_thread(timed_thread):
def run(self):
sleep(15)
print "st DONE:\t%f" % (time() - time_start)
# Thread that increments a number for a while
class spin_thread(timed_thread):
def run(self):
x = 1
while x < 120000000:
x += 1
print "sp DONE:\t%f" % (time() - time_start)
# Thread that calls math.factorial with a large number
class factorial_thread(timed_thread):
def run(self):
factorial(50000)
print "ft DONE:\t%f" % (time() - time_start)
# the tests
print
print "sleep_thread test"
time_start = time()
st = sleep_thread(time_start)
st.start()
print "st.start:\t%f" % (time() - time_start)
st.join(2)
print "st.join:\t%f" % (time() - time_start)
print "sleep alive:\t%r" % st.isAlive()
print
print "spin_thread test"
time_start = time()
sp = spin_thread(time_start)
sp.start()
print "sp.start:\t%f" % (time() - time_start)
sp.join(2)
print "sp.join:\t%f" % (time() - time_start)
print "sp alive:\t%r" % sp.isAlive()
print
print "factorial_thread test"
time_start = time()
ft = factorial_thread(time_start)
ft.start()
print "ft.start:\t%f" % (time() - time_start)
ft.join(2)
print "ft.join:\t%f" % (time() - time_start)
print "ft alive:\t%r" % ft.isAlive()
Run Code Online (Sandbox Code Playgroud)
以下是CentOS x64上Python 2.6.5的输出:
sleep_thread test
st.start: 0.000675
st.join: 2.006963
sleep alive: True
spin_thread test
sp.start: 0.000595
sp.join: 2.010066
sp alive: True
factorial_thread test
ft DONE: 4.475453
ft.start: 4.475589
ft.join: 4.475615
ft alive: False
st DONE: 10.994519
sp DONE: 12.054668
Run Code Online (Sandbox Code Playgroud)
我在CentOS x64上的python 2.6.5上尝试过这个,在Windows x86上使用2.7.2,并且在线程完成执行之前,因子线程不会从它们的任何一个开始返回.
我也在Windows x86上尝试使用PyPy 1.8.0,结果略有不同.开始确实立即返回,但是加入不会超时!
sleep_thread test
st.start: 0.001000
st.join: 2.001000
sleep alive: True
spin_thread test
sp.start: 0.000000
sp DONE: 0.197000
sp.join: 0.236000
sp alive: False
factorial_thread test
ft.start: 0.032000
ft DONE: 9.011000
ft.join: 9.012000
ft alive: False
st DONE: 12.763000
Run Code Online (Sandbox Code Playgroud)
尝试了IronPython 2.7.1,它产生了预期的结果.
sleep_thread test
st.start: 0.023003
st.join: 2.028122
sleep alive: True
spin_thread test
sp.start: 0.003014
sp.join: 2.003128
sp alive: True
factorial_thread test
ft.start: 0.002991
ft.join: 2.004105
ft alive: True
ft DONE: 5.199295
sp DONE: 5.734322
st DONE: 10.998619
Run Code Online (Sandbox Code Playgroud)
由于Global Interpreter Lock,线程通常只允许在Python中交错不同的东西,而不是同时发生不同的事情.
如果你看一下Python字节码:
from math import factorial
def fac_test(x):
factorial(x)
import dis
dis.dis(fac_test)
Run Code Online (Sandbox Code Playgroud)
你得到:
4 0 LOAD_GLOBAL 0 (factorial)
3 LOAD_FAST 0 (x)
6 CALL_FUNCTION 1
9 POP_TOP
10 LOAD_CONST 0 (None)
13 RETURN_VALUE
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,调用math.factorial是Python字节码级别的单个操作(6 CALL_FUNCTION) - 它在C factorial中实现.由于它的工作类型而没有释放GIL(请参阅我的答案的评论),所以Python在运行时不会切换到其他线程,并且您得到了您观察到的结果.
| 归档时间: |
|
| 查看次数: |
681 次 |
| 最近记录: |