Nov-11-2017, 12:40 PM
Hi All
The following code is my son's homework for an A level he has just begun. The activity he is having trouble with is this:
Activity 2 – Change the following code so that Thread 1 stops after 1 second, Thread 2 after 2 seconds etc.
import time
from threading import Thread
def myfunc(i):
print("\nsleeping 5 sec from thread %d" % i)
time.sleep(5)
print ("\nfinished sleeping from thread %d" % i)
for i in range(10):
t = Thread(target=myfunc, args=(i,))
t.start()
He thought by changing the time.sleep line to: time.sleep(i)
that it should work, but it only gives the result below and only thread 0 seems to stop after 0 seconds with the rest (1-9) all taking about 1 second:
sleeping 5 sec from thread 0
sleeping 5 sec from thread 1
finished sleeping from thread 0
sleeping 5 sec from thread 2
sleeping 5 sec from thread 3
sleeping 5 sec from thread 4
sleeping 5 sec from thread 5
sleeping 5 sec from thread 6
sleeping 5 sec from thread 7
sleeping 5 sec from thread 8
sleeping 5 sec from thread 9
finished sleeping from thread 1
finished sleeping from thread 2
finished sleeping from thread 3
finished sleeping from thread 4
finished sleeping from thread 5
finished sleeping from thread 6
finished sleeping from thread 7
finished sleeping from thread 8
finished sleeping from thread 9
I'm trying to be a cool dad and help him but I can't see where he is going wrong and his online tutor is not contactable. I can't tell you how great it would be if someone could point us in the right direction. He would be asking himself but he is a games thingy in Bradford today. Thanks - Maxspencer
The following code is my son's homework for an A level he has just begun. The activity he is having trouble with is this:
Activity 2 – Change the following code so that Thread 1 stops after 1 second, Thread 2 after 2 seconds etc.
import time
from threading import Thread
def myfunc(i):
print("\nsleeping 5 sec from thread %d" % i)
time.sleep(5)
print ("\nfinished sleeping from thread %d" % i)
for i in range(10):
t = Thread(target=myfunc, args=(i,))
t.start()
He thought by changing the time.sleep line to: time.sleep(i)
that it should work, but it only gives the result below and only thread 0 seems to stop after 0 seconds with the rest (1-9) all taking about 1 second:
sleeping 5 sec from thread 0
sleeping 5 sec from thread 1
finished sleeping from thread 0
sleeping 5 sec from thread 2
sleeping 5 sec from thread 3
sleeping 5 sec from thread 4
sleeping 5 sec from thread 5
sleeping 5 sec from thread 6
sleeping 5 sec from thread 7
sleeping 5 sec from thread 8
sleeping 5 sec from thread 9
finished sleeping from thread 1
finished sleeping from thread 2
finished sleeping from thread 3
finished sleeping from thread 4
finished sleeping from thread 5
finished sleeping from thread 6
finished sleeping from thread 7
finished sleeping from thread 8
finished sleeping from thread 9
I'm trying to be a cool dad and help him but I can't see where he is going wrong and his online tutor is not contactable. I can't tell you how great it would be if someone could point us in the right direction. He would be asking himself but he is a games thingy in Bradford today. Thanks - Maxspencer