测试多线程的时候 出现了这种情况 怎么解
import threadpool
import time
def sayHello(name):
print("hello", name)
time.sleep(2)
start_time = time.time()
pool = threadpool.ThreadPool(10)
requests=threadpool.makeRequests(sayHello, [a for a in 'AHHHHHHHHHHHHHHAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'])
[pool.putRequest(req) for req in requests]
pool.wait()
print ('%d second'% (time.time()-start_time))

import threading
import time
class MyThread(threading.Thread):
def __init__(self, n):
super(MyThread, self).__init__()
self.n = n
def run(self):
print('task', self.n)
time.sleep(1)
print('task '+self.n + ' remains 2s')
time.sleep(1)
print('task ' + self.n + ' remains 1s')
time.sleep(1)
print('task ' + self.n + ' remains 0s')
if __name__=='__main__':
for i in range(10):
t = MyThread('t'+str(i))
t.start()
本文探讨了在使用ThreadPool和自定义线程类进行多线程测试时遇到的问题,提供了问题排查和优化并行执行的解决方案,适合深入理解并发编程的读者。
746

被折叠的 条评论
为什么被折叠?



