首页 新闻 搜索 专区 学院

关于python 多线程的问题,求大佬

0
悬赏园豆:20 [已关闭问题] 关闭于 2018-10-10 13:30

本人开启100个线程工作,重写了run方法,结束后循环再次开启100个线程,一直这样循环,在开启到一定数量的时候(本机所能开启的最大线程数)提示报错:can't start new thread
看了一下,原来开启的线程虽然工作结束了,但是所占的资源没释放,造成这个原因,网上查找无果,求大佬指导,上代码:

import threading , time
from queue import Queue

class BdSpider(threading.Thread):
    def __init__(self, waiting):
        super(BdSpider, self).__init__()
        self.waiting = waiting
        self.flag = True
    def run(self):
        while self.flag:
            ipone = self.waiting.get()
            self.waiting.task_done()
    def ssss(self):
        self.flag = False

def ipAction():
    for i in range(10):
        aaa = []
        wait_list = Queue()
        thread_num = 20        
        for keyip in range(2000):
            if keyip:
                wait_list.put(keyip)#往Queue添加
        for ii in range(100):
            print(ii + i*100)
            thread = BdSpider(wait_list)
            thread.setDaemon(True)#设置守护进程
            thread.start()
            aaa.append(thread)
        wait_list.join()
        for ii in aaa:
            ii.ssss()
        print("-------------------------------")
        print(threading.active_count())#打印当前线程数
ipAction()
print("main ------")
print(threading.active_count())

打印线程数也是一直增加到极限

报错:

Traceback (most recent call last):
  File "xiancheng.py", line 36, in <module>
    ipAction()
  File "xiancheng.py", line 29, in ipAction
    thread.start()
  File "C:\Users\hujjjj\AppData\Local\Programs\Python\Python36-32\lib\threading.py", line 846, in start
    _start_new_thread(self._bootstrap, ())
RuntimeError: can't start new thread
自说自话唉的主页 自说自话唉 | 初学一级 | 园豆:193
提问于:2018-10-09 16:04
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册