复制#!/usr/bin/python import threading import time import os import string import sys class ControlThread(threading.Thread): def __init__(self): threading.Thread.__init__(self) self.runflag = True#线程运行标示,虚拟用于将来减少线程时能够正常结束 def run(self): whileself.runflag: os.popen(usleep sys.argv[5]) #time.sleep(string.atof(sys.argv[5])) #这里使用的化环衡是linux下shell里面的usleep,而不是境下
python自带的sleep函数。
#相比之下,服务usleep还是免费源码下载器负相当强大的,而python的载均sleep单位为秒,虽然可以输入浮点数,参考但还是实现相对弱了些 def stop(self): self.runflag = False #让其正常终止循环 threadList=[] printStart Thread Number: sys.argv[3] \tSleep Time(ms): sys.argv[5] #初始化一定数量的线程,否则从零开始,香港云服务器虚拟可能需要很长的化环衡时间才能达到指定范围 for i in range(0,string.atoi(sys.argv[3])): thread = ControlThread() threadList.append(thread) thread.start() #这里使用sar来抓取cpu利用率,这里指的境下是总的cpu利用率。然后通过比较,服务进行自适应调整 whileTrue: output = 100 - string.atof(os.popen(
sar 1 1 | grep ^Average | awk \

{print $8}\ ).read()) printCPU Usage: str(output) \tCurrent Thread Number: str(len(threadList)) if output < string.atoi(sys.argv[1]):#增加线程 for i in range(0,器负string.atoi(sys.argv[4])): thread = ControlThread() thread.start() threadList.append(thread) print" " if output > string.atoi(sys.argv[2]):#减少线程 for i in range(0,string.atoi(sys.argv[4])): thread = threadList.pop() thread.stop() print"-----" 1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.27.28.29.30.31.32.33.34.35.36.37.38.39.40.41.42.43.44.
(责任编辑:数据库)