#!/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 &