First page Back Continue Last page Overview Graphics

Exercise

Rewrite the function from threading, which sleeps for 5 seconds before it finishes.

Call it 10 times as a Process, i.e. creating 10 process.

import time

from multiprocessing import Process

def sleeper(i):

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

time.sleep(5)

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

for i in range(10):

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

t.start()

process 1: falling asleep

process 4: falling asleep

process 0: falling asleep

process 2: falling asleep

process 3: falling asleep

process 5: falling asleep

process 6: falling asleep

process 7: falling asleep

process 9: falling asleep

process 8: falling asleep

process 3: woke up

process 4: woke up

process 0: woke up

process 1: woke up

process 2: woke up

process 5: woke up

process 6: woke up

process 7: woke up

process 9: woke up

process 8: woke up