taijitu/taijitu.py

92 lines
2.3 KiB
Python
Raw Normal View History

from abots.helpers import generator
from psutil import Process as PSProcess
from queue import Queue, Empty
from time import sleep, monotonic as time
from os import getloadavg
from collections import deque
from multiprocessing import cpu_count
actions = dict()
events = Queue()
last = time()
ticks = deque()
ticks.append(0)
max_loadavg = float(cpu_count())
threshold = 15.0
max_cpu = 25.0
sleeping = 0.001
sleepings = list()
stats = list()
magic = 0
step = 0.0001
max_ticks = 0
loops = 0
proc = PSProcess()
cpu = proc.cpu_percent()
def register(func):
actions[func.__name__] = generator(func)
return func
@register
def push():
message = (yield)
time1 = round(sleeping, 6)
time2 = round(magic, 6)
print("push", message, time1, time2, loops, max_ticks, cpu)
event = "pull", ticks[-1]
events.put_nowait(event)
@register
def pull():
message = (yield)
time1 = round(sleeping, 6)
time2 = round(magic, 6)
print("pull", message, time1, time2, loops, max_ticks, cpu)
event = "push", ticks[-1]
events.put_nowait(event)
seed = "push", ticks[-1]
events.put_nowait(seed)
done = time() + 60
prev_cpu = 0
prev_loadavg = 0
while True: #time() < done:
if time() >= last + 1:
ticks.append(0)
last = time()
loadavg = getloadavg()[0]
diff_loadavg = loadavg - prev_loadavg
prev_loadavg = loadavg
if loadavg >= max_loadavg and diff_loadavg > 0:
sleeping = sleeping + step * 10
else:
cpu = proc.cpu_percent()
diff_cpu = cpu - prev_cpu
prev_cpu = cpu
if cpu >= max_cpu and diff_cpu > 0:
sleeping = sleeping + step * 10
elif cpu >= threshold and diff_cpu > 0:
sleeping = sleeping + step
elif sleeping - step >= 0 and diff_cpu < 0:
sleeping = sleeping - step
sleepings.append(sleeping)
stat = sum(sleepings) / len(sleepings)
stats.append(stat)
magic = sum(stats) / len(stats)
max_ticks = max(ticks)
loops = loops + 1
try:
event = events.get_nowait()
source, message = event
action = actions.get(source)
if action:
action().send(message)
except Empty:
break
sleep(sleeping)
ticks[-1] = ticks[-1] + 1