我试图在Python中同时运行两个长时间运行的操作。它们都在相同的数据集上运行,但不要修改它们。我发现一个线程实现运行速度比一个接一个地运行它们要慢。在Python中对可串行化操作进行线程化处理比按顺序执行它们要慢
我创建了一个简化的示例来展示我正在经历的事情。
运行此代码并注释第46行(导致它执行线程操作)会导致我的计算机上的运行时间约为1:01(分钟:秒)。我看到两个CPU在整个运行时间运行约50%。
注释掉第47行(导致顺序计算)将导致大约35秒的运行时间,其中1个CPU在整个运行时间内被固定为100%。
两次运行都会导致完整的计算完成。
from datetime import datetime
import threading
class num:
def __init__(self):
self._num = 0
def increment(self):
self._num += 1
def getValue(self):
return self._num
class incrementNumber(threading.Thread):
def __init__(self, number):
self._number = number
threading.Thread.__init__(self)
def run(self):
self.incrementProcess()
def incrementProcess(self):
for i in range(50000000):
self._number.increment()
def runThreaded(x, y):
x.start()
y.start()
x.join()
y.join()
def runNonThreaded(x, y):
x.incrementProcess()
y.incrementProcess()
def main():
t = datetime.now()
x = num()
y = num()
incrementX = incrementNumber(x)
incrementY = incrementNumber(y)
runThreaded(incrementX, incrementY)
#runNonThreaded(incrementX, incrementY)
print x.getValue(), y.getValue()
print datetime.now() - t
if __name__=="__main__":
main()
反正我不使用Python,但我不明白为什么那个可怜的互斥体是必须的。难道Python不会在线程本地存储中存储上下文对象或指针吗?为每个线程找到其他自己的解释器实例的其他方法? – 2012-04-17 17:50:56
从下面重复自己:有[Python的实现](http://wiki.python.org/jython/WhyJython)没有GIL。至于“每个线程都有自己的解释器实例” - 这就是“多处理”(答案中提到的)所做的。 – 2012-04-17 17:52:49
有些开发人员不想与其他进程共享5GB数据缓冲区!就像你说的那样,Anywy似乎确实存在GILless pythons,所以我的回答是误导性的,即使不是错的。 – 2012-04-17 18:06:39