Python Forum
Threading in Python 3 homework
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Threading in Python 3 homework
#1
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
Reply
#2
Quote: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.
This seems correct to me.
Each thread stops one second after the previous, yes, but stops a total of i seconds after its creation total.
One thread stops every second.
Think of all the threads being essentially created simultaneously (they are all created in milliseconds). 9 seconds later the thread with i=9 ends.
Reply
#3
Thanks Mekire,

I think you might be right. All this time we were looking at this like idiots. Thanks for pointing out our fatal flaw. Cheers, Maxspencer.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  HELP in python homework makashito 4 3,913 Oct-12-2021, 10:12 AM
Last Post: buran
  CyperSecurity Using Python HomeWork ward1995 1 1,958 Jul-08-2021, 03:55 PM
Last Post: buran
Exclamation urgent , Python homework alm 2 2,301 May-09-2021, 11:19 AM
Last Post: Yoriz
  Homework with python Johnsonmfw 1 1,688 Sep-20-2020, 04:03 AM
Last Post: ndc85430
  Python Homework Help *Urgent GS31 2 2,581 Nov-24-2019, 01:41 PM
Last Post: ichabod801
  Python Homework Question OrcDroid123 1 2,368 Sep-01-2019, 08:44 AM
Last Post: buran
  Python homework / functions sunhyunshine 1 2,451 May-11-2019, 05:37 PM
Last Post: MrTheOne
  python homework help ASAP gk34332 1 2,970 Mar-13-2019, 07:27 PM
Last Post: ichabod801
  Python homework assigment makisha 3 3,277 Feb-28-2019, 10:21 PM
Last Post: Yoriz
  Python Homework Help beepBoop123 2 3,045 Dec-12-2018, 06:25 PM
Last Post: beepBoop123

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020