登录
注册
开源
企业版
高校版
搜索
帮助中心
使用条款
关于我们
开源
企业版
高校版
私有云
模力方舟
登录
注册
代码拉取完成,页面将自动刷新
开源项目
>
DevOps/运维/网管
>
DevOps工具
&&
捐赠
捐赠前请先登录
取消
前往登录
扫描微信二维码支付
取消
支付完成
支付提示
将跳转至支付宝完成支付
确定
取消
Watch
不关注
关注所有动态
仅关注版本发行动态
关注但不提醒动态
413
Star
1.1K
Fork
459
张其川
/
CheungSSH
代码
Issues
11
Pull Requests
1
Wiki
统计
流水线
服务
质量分析
Jenkins for Gitee
腾讯云托管
腾讯云 Serverless
悬镜安全
阿里云 SAE
Codeblitz
SBOM
我知道了,不再自动展开
更新失败,请稍后重试!
移除标识
内容风险标识
本任务被
标识为内容中包含有代码安全 Bug 、隐私泄露等敏感信息,仓库外成员不可访问
CheungSSHPool的调用会导致系统线程资源耗尽
待办的
#IFOJ4
JohnSaxon
创建于
2017-10-18 18:28
CheungSSHPool的调用会导致系统线程资源耗尽 第一次提issue,不知道怎么贴代码 问题都写在代码注释里了,这次将就看吧 后续我会就提出的bug修改部分代码 --- 文件 cheungssh_auto_thread.py class AutoGetThreadNum: def __init__(self): self.cpu=100 ### 1.cpu数目最好动态检测,不要写死,或者在写一个方法修改cpu数目 self.thread=5 def auto_thread(self): #### 2.cpu数目不动态检测的话,这个方法其实是没有用的,进程数始终是200,每次申请200而且有些情况下不释放,例如执行top命令,就会导致系统线程耗尽,影响整个系统 if self.cpu>90: self.thread=200 elif self.cpu>80: self.thread=150 elif self.cpu>70: self.thread=100 elif self.cpu>60: self.thread=50 elif self.cpu>50: self.thread=40 elif self.cpu>30: self.thread=20 elif self.cpu>20: self.thread=5 return self.thread --- 文件 cheungssh_thread_queue.py import threading,Queue,time from cheungssh_auto_thread import AutoGetThreadNum from cheungssh_modul_controler import CheungSSHControler from cheungssh_error import CheungSSHError import os,sys,json os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings") sys.path.append('/home/cheungssh/mysite') from django.core.cache import cache class CheungSSHThreadAdmin(object): def __init__(self): self.REDIS=cache.master_client def run(self,parameter={}): cheungssh_info={"status":True,"content":""} try: task_type=parameter["task_type"] tid=parameter["tid"] multi_thread=parameter["multi_thread"] if not type(multi_thread) ==type(False):raise CheungSSHError("CHB0000000010") if task_type=="cmd": cmd=parameter["cmd"] servers=parameter["servers"] if not type(servers)==type([]):raise CheungSSHError("CHB0000000011") total="total.%s" % tid current="current.%s" %tid self.REDIS.set(total,len(servers)) self.REDIS.set(current,0) #CheungSSHConnector.progress[total]=len(servers) #CheungSSHConnector.progress[current]=0 if multi_thread: pool=CheungSSHPool() for s in servers: controler=CheungSSHControler() param={"cmd":cmd,"sid":s,"tid":tid} pool.add_task(controler.command_controler,param) else: for s in servers: controler=CheungSSHControler() controler.command_controler(cmd=cmd,sid=s,tid=tid) elif task_type=="file": pass else: raise CheungSSHError("CHB0000000009") except Exception,e: cheungssh_info["content"]=str(e) cheungssh_info["status"]=False print cheungssh_info return cheungssh_info class CheungSSHThread(threading.Thread): def __init__(self,queue): threading.Thread.__init__(self) self.queue=queue self.daemon=True self.start() ### 5. start自动调用run()方法 def run(self): while True:### 4. 这里,每起一个thread,这个thread都不会自动退出,最后导致系统线程被耗尽 try: func,kws=self.queue.get() func(**kws) except Exception,e: print e pass self.queue.task_done() class CheungSSHPool(AutoGetThreadNum): def __init__(self): AutoGetThreadNum.__init__(self) self.thread_num=self.auto_thread() self.queue=Queue.Queue(self.thread_num) for i in range(self.thread_num): CheungSSHThread(self.queue) ### 3. 每次会申请200个线程,而每一个的thread的run方法是一个没有break的死循环,线程永远不会退出。详见 ### 4. def add_task(self,func,dict): self.queue.put((func,dict)) def all_complete(self): self.queue.join() def test_func(**kws): time.sleep(0.5) print "哈哈,这里是你穿的参数",kws if __name__=='__main__': p=CheungSSHPool() for i in range(20): p.add_task(test_func,{"username":i}) p.all_complete()
CheungSSHPool的调用会导致系统线程资源耗尽 第一次提issue,不知道怎么贴代码 问题都写在代码注释里了,这次将就看吧 后续我会就提出的bug修改部分代码 --- 文件 cheungssh_auto_thread.py class AutoGetThreadNum: def __init__(self): self.cpu=100 ### 1.cpu数目最好动态检测,不要写死,或者在写一个方法修改cpu数目 self.thread=5 def auto_thread(self): #### 2.cpu数目不动态检测的话,这个方法其实是没有用的,进程数始终是200,每次申请200而且有些情况下不释放,例如执行top命令,就会导致系统线程耗尽,影响整个系统 if self.cpu>90: self.thread=200 elif self.cpu>80: self.thread=150 elif self.cpu>70: self.thread=100 elif self.cpu>60: self.thread=50 elif self.cpu>50: self.thread=40 elif self.cpu>30: self.thread=20 elif self.cpu>20: self.thread=5 return self.thread --- 文件 cheungssh_thread_queue.py import threading,Queue,time from cheungssh_auto_thread import AutoGetThreadNum from cheungssh_modul_controler import CheungSSHControler from cheungssh_error import CheungSSHError import os,sys,json os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings") sys.path.append('/home/cheungssh/mysite') from django.core.cache import cache class CheungSSHThreadAdmin(object): def __init__(self): self.REDIS=cache.master_client def run(self,parameter={}): cheungssh_info={"status":True,"content":""} try: task_type=parameter["task_type"] tid=parameter["tid"] multi_thread=parameter["multi_thread"] if not type(multi_thread) ==type(False):raise CheungSSHError("CHB0000000010") if task_type=="cmd": cmd=parameter["cmd"] servers=parameter["servers"] if not type(servers)==type([]):raise CheungSSHError("CHB0000000011") total="total.%s" % tid current="current.%s" %tid self.REDIS.set(total,len(servers)) self.REDIS.set(current,0) #CheungSSHConnector.progress[total]=len(servers) #CheungSSHConnector.progress[current]=0 if multi_thread: pool=CheungSSHPool() for s in servers: controler=CheungSSHControler() param={"cmd":cmd,"sid":s,"tid":tid} pool.add_task(controler.command_controler,param) else: for s in servers: controler=CheungSSHControler() controler.command_controler(cmd=cmd,sid=s,tid=tid) elif task_type=="file": pass else: raise CheungSSHError("CHB0000000009") except Exception,e: cheungssh_info["content"]=str(e) cheungssh_info["status"]=False print cheungssh_info return cheungssh_info class CheungSSHThread(threading.Thread): def __init__(self,queue): threading.Thread.__init__(self) self.queue=queue self.daemon=True self.start() ### 5. start自动调用run()方法 def run(self): while True:### 4. 这里,每起一个thread,这个thread都不会自动退出,最后导致系统线程被耗尽 try: func,kws=self.queue.get() func(**kws) except Exception,e: print e pass self.queue.task_done() class CheungSSHPool(AutoGetThreadNum): def __init__(self): AutoGetThreadNum.__init__(self) self.thread_num=self.auto_thread() self.queue=Queue.Queue(self.thread_num) for i in range(self.thread_num): CheungSSHThread(self.queue) ### 3. 每次会申请200个线程,而每一个的thread的run方法是一个没有break的死循环,线程永远不会退出。详见 ### 4. def add_task(self,func,dict): self.queue.put((func,dict)) def all_complete(self): self.queue.join() def test_func(**kws): time.sleep(0.5) print "哈哈,这里是你穿的参数",kws if __name__=='__main__': p=CheungSSHPool() for i in range(20): p.add_task(test_func,{"username":i}) p.all_complete()
评论 (
1
)
登录
后才可以发表评论
状态
待办的
待办的
进行中
已完成
已关闭
负责人
未设置
标签
未设置
标签管理
里程碑
未关联里程碑
未关联里程碑
Pull Requests
未关联
未关联
关联的 Pull Requests 被合并后可能会关闭此 issue
分支
未关联
未关联
master
开始日期   -   截止日期
-
置顶选项
不置顶
置顶等级:高
置顶等级:中
置顶等级:低
优先级
不指定
严重
主要
次要
不重要
参与者(1)
Python
1
https://gitee.com/CheungSSH_OSC/CheungSSH.git
git@gitee.com:CheungSSH_OSC/CheungSSH.git
CheungSSH_OSC
CheungSSH
CheungSSH
点此查找更多帮助
搜索帮助
Git 命令在线学习
如何在 Gitee 导入 GitHub 仓库
Git 仓库基础操作
企业版和社区版功能对比
SSH 公钥设置
如何处理代码冲突
仓库体积过大,如何减小?
如何找回被删除的仓库数据
Gitee 产品配额说明
GitHub仓库快速导入Gitee及同步更新
什么是 Release(发行版)
将 PHP 项目自动发布到 packagist.org
评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册