From b3a44bc9f2799c0a73ec17e9ad8745c4604cfa70 Mon Sep 17 00:00:00 2001 From: aewens Date: Sat, 10 Aug 2019 20:11:37 -0500 Subject: [PATCH] Initial commit, implements dynamic event loop to optimize speed and minimize resources --- .gitignore | 4 +++ LICENSE | 2 +- __init__.py | 0 taijitu.py | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 __init__.py create mode 100644 taijitu.py diff --git a/.gitignore b/.gitignore index e61bca2..a6e00dc 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,7 @@ +# Application-specific +abots +*.vim + # ---> Python # Byte-compiled / optimized / DLL files __pycache__/ diff --git a/LICENSE b/LICENSE index b2a9c51..4a43692 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -Copyright (c) . All rights reserved. +Copyright (c) 2019 Austin Ewens. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/taijitu.py b/taijitu.py new file mode 100644 index 0000000..462cf2c --- /dev/null +++ b/taijitu.py @@ -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