First page Back Continue Last page Overview Graphics

Example: threading module

Output:

thread 0: falling asleep

thread 1: falling asleep

thread 2: falling asleep

thread 3: falling asleep

thread 4: falling asleep

thread 5: falling asleep

thread 6: falling asleep

thread 7: falling asleep

thread 8: falling asleep

thread 9: falling asleep

thread 0: woke up

thread 1: woke up

thread 2: woke up

thread 3: woke up

thread 4: woke up

thread 6: woke up

thread 9: woke up

thread 7: woke up

thread 8: woke up

thread 5: woke up

import time

from threading import Thread

def sleeper(i):

print("thread %d: falling asleep" % i)

time.sleep(5)

print("thread %d: woke up" % i)

for i in range(10):

t = Thread(target=sleeper, args=(i,))

t.start()