小编W1N*_*Zr0的帖子

为什么Python的math.factorial不能与线程一起使用?

为什么math.factorial在一个线程中表现得如此奇怪?

这是一个例子,它创建了三个线程:

  • 只是睡了一会儿的线程
  • 一段时间增加int的线程
  • 在大数字上执行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 …
Run Code Online (Sandbox Code Playgroud)

python multithreading factorial blocking gil

3
推荐指数
1
解决办法
681
查看次数

标签 统计

blocking ×1

factorial ×1

gil ×1

multithreading ×1

python ×1