I am have problem with this THREDING example. I have it working pretty good, but my problem is after it shows all 100 student threads. I am trying to put 20 random students in five different classes, but no matter what I do I can't seem to get the loop to work. If someone could please give me any direction on this, I would greatly appreciate it.
import random, time
from threading import Thread
class Students(Thread):
''' Represents a sleepy thread.'''
def __init__(self, number, sleepMax):
''' Creates a thread with a given Name
and a random sleep interval less than the maximum. '''
Thread.__init__(self, name = "Student " + str(number))
self._RequestInterval = random.randint(1, sleepMax)
def run(self):
'''Prints the thread's name and sleep interval and sleep
for that interval. Print the name again at wake-up. '''
count = 1
course = 1
print(" %s, Awaiting Request: %d seconds" % \
( self.getName(), self._RequestInterval))
time.sleep(self._RequestInterval)
if count == 20:
print("%s Has A Spot Obtained in Class" % self.getName(), + course)
print("Class", course, "has reached it limit!")
count += 1
course =+ 1
else:
#print("Class ", course, " is full.")
print("%s Has A Spot Obtained in Class" % self.getName(), + course)
def main():
''' Creates the user's number of threads with sleep
interval less than the user's maximum. Then start
the threads'''
NumberOfStudents = 100
RequestTimer = 5
threadList = []
for count2 in range(NumberOfStudents):
threadList.append(Students(count2 + 1, RequestTimer))
for thread in threadList: thread.start()
main()
I have even tried running my variable outside the class, but crashes.