First page Back Continue Last page Overview Image

Übung

Schreiben Sie die Funktion aus dem threading-Beispiel um, die 5 Sekunden geschlafen hat und dann endete. rufen Sie sie dann 10 Mal als Prozess auf.

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