[英]Python creating multiple threads and creating processing time
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)
这与我
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)
you can't start a thread after you killed it. 杀死线程后,您将无法启动线程。
you iterate over self.threads
10 times ( for i in range(10)
). 您遍历
self.threads
10次( 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)
: 尝试
for i in range(10)
添加下一行:
self.threads = []
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.