压力测试中监听多个进程的CPU,MEM(多线程)

#!/usr/bin/python

import time, threading
import random
import struct
import gevent
import signal
import os
from gevent import socket
import psutil
import sys

thread_num = 2  #open thread num
is_exit = True
time_out_count = 0

thread_count = range(0,thread_num)

def listen(FL,PID):
    f = open(FL,'w')
    #with open(FL) as f:
    i = 1
    while i:
            p1 = psutil.Process(PID)
            cpu1 = p1.cpu_percent(interval=1)
            mem = p1.memory_percent()
            #print(mem)

            f.write('%f %f\n' % (cpu1,mem))
            #print(PID)
           
            f.flush()
            os.fsync(f)
    f.close

def main1(self):
    pd_back = 2825
    fl_back = 'back.txt'
    print('back start')
    listen(fl_back,pd_back)
def main2(self):
    pd_front = 2839
    fl_front = 'front.txt'
    print('front start')
    listen(fl_front,pd_front)


ncli = range(0,thread_num)
threads = []
def press_test():
    try:
        for i in ncli:
            t1 = threading.Thread(target=main1, args='1')
            t2 = threading.Thread(target=main2, args='2')
            t1.setDaemon(True)
            t2.setDaemon(True)
            threads.append(t1)
            threads.append(t2)
        for i in ncli:
            threads[i].start()
        #for i in ncli:
        #    threads[i].join()
    except KeyboardInterrupt:
        print 'end'


if __name__ == '__main__':
    global start_time
    start_time = 0
    start_time = time.time()
    press_test()
    while True:
        alive = False
        for i in ncli:
            alive = alive or threads[i].isAlive()
            time.sleep(0.01)
        if not alive:
            break

可后台:nohup python *.py &

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值