threading.Thread の使用例を次に示します。クラス名を独自のものに、run 関数を独自のものに置き換えるだけです。スレッド化は、IO が制限されたアプリケーションに最適であり、実際に高速化できることに注意してください。標準の python での計算に厳密に pythong スレッドを使用しても、一度に 1 つのスレッドしか計算できないため、役に立ちません。
import threading, time
class Ping(threading.Thread):
def __init__(self, multiple):
threading.Thread.__init__(self)
self.multiple = multiple
def run(self):
#sleeps 3 seconds then prints 'pong' x times
time.sleep(3)
printString = 'pong' * self.multiple
pingInstance = Ping(3)
pingInstance.start() #your run function will be called with the start function
print "pingInstance is alive? : %d" % pingInstance.isAlive() #will return True, or 1
print "Number of threads alive: %d" % threading.activeCount()
#main thread + class instance
time.sleep(3.5)
print "Number of threads alive: %d" % threading.activeCount()
print "pingInstance is alive?: %d" % pingInstance.isAlive()
#isAlive returns false when your thread reaches the end of it's run function.
#only main thread now