Initial commit, implements dynamic event loop to optimize speed and minimize resources
This commit is contained in:
parent
18e396f8b4
commit
b3a44bc9f2
|
@ -1,3 +1,7 @@
|
||||||
|
# Application-specific
|
||||||
|
abots
|
||||||
|
*.vim
|
||||||
|
|
||||||
# ---> Python
|
# ---> Python
|
||||||
# Byte-compiled / optimized / DLL files
|
# Byte-compiled / optimized / DLL files
|
||||||
__pycache__/
|
__pycache__/
|
||||||
|
|
2
LICENSE
2
LICENSE
|
@ -1,4 +1,4 @@
|
||||||
Copyright (c) <year> <owner> . All rights reserved.
|
Copyright (c) 2019 Austin Ewens. All rights reserved.
|
||||||
|
|
||||||
Redistribution and use in source and binary forms, with or without modification,
|
Redistribution and use in source and binary forms, with or without modification,
|
||||||
are permitted provided that the following conditions are met:
|
are permitted provided that the following conditions are met:
|
||||||
|
|
|
@ -0,0 +1,91 @@
|
||||||
|
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
|
Loading…
Reference in New Issue