Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am getting an error when I run my code: "threads can only be started once"

it has to do with my for I in range(10)

I kill the threads right after I start it so in theory all threads should be killed before going back into the next loop to create another set of threads.

What I am trying to do is to get the time it takes to run a thread

def __startThreads__(self):
        print("How Many =", self.howMany)
        start1 = time()
        for i in range(10):
            start2 = time()
            for i in range(self.howMany):
                self.threads.append( ThreadEntity( str(i), iAmTheProgramCode ) )
            for each in self.threads: 
                each.start()
            for each in self.threads:
                each.killThreadEntity()
            stop2 = time()
            threadtime = stop2 - start2
            print(threadtime)
        stop1 = time()
        threadTotalTime = stop1 - start1
        print(threadTotalTime)
share|improve this question
    
Next time, please format your code appropriately ;-) I fixed that for you, basically it was missing an indent. –  Uli Köhler Jan 28 at 23:52
    
How do you know it has to do with the line: 'for I in range(10)'? –  BakaKuna Jan 29 at 0:00
    
if I put for I in range(1) it doesn't have a problem because it only tries 1 iteration of the loop but when I increase it to 2 it crashes –  John Jan 29 at 0:02
    
Here is the error I get: –  John Jan 29 at 0:04
    
Exception in Tkinter callback Traceback (most recent call last): File "C:\Python33\lib\tkinter_init_.py", line 1475, in call return self.func(*args) File "C:\Users\cslaptop\Desktop\SimpleVirtualShell.py", line 133, in startThreads each.start() File "C:\Python33\lib\threading.py", line 836, in start raise RuntimeError("threads can only be started once") RuntimeError: threads can only be started once –  John Jan 29 at 0:05

1 Answer 1

up vote 1 down vote accepted

you can't start a thread after you killed it.

you iterate over self.threads 10 times (for i in range(10)). and in each iteration you start all the threads in it (and append some new ones but you also iterate over the previous which were killed).

I think that what you ment to do is to empty the threads list before each iteration.

try add the next line after for i in range(10):

self.threads = []
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.