Seite 1 von 1

Regelkreis...

Verfasst: Mittwoch 16. Juli 2014, 11:34
von jens
Auch wenn ich mich gerade Frage, ob mir threading.Timer() nicht besser hilft, hab ich hier eine Art Regelkreis gemacht:

Code: Alles auswählen

#!/usr/bin/env python

import time
import threading
import sys

class InputPoll(threading.Thread):
    def __init__ (self, load):
        self.load = load
        threading.Thread.__init__(self)

    def run(self):
        print "Start Thread..."
        while True:
            char = sys.stdin.read(1)
            if char=="+":
                self.load *= 1.1
            elif char=="-":
                self.load *= 0.9
            elif char in ("q","x"):
                print "Exit!"
                break
            else:
                print "Nur +, - oder q, x eingeben!"

def calc_new_count(burst_count, loop_count, target_loop_count):
    a = float(burst_count) / float(loop_count) * target_loop_count
    b = (burst_count - a)
    new_burst_count = burst_count + b
    new_burst_count = int(round(new_burst_count))
    if new_burst_count<1:
        new_burst_count=1
    return new_burst_count

def regelkreis():

    load = 0.01
    input_thread = InputPoll(load)
    input_thread.start()

    update_duration = 1
    next_update = time.time() + update_duration
    burst_count = 30
    loop_count = 0
    target_loop_count = 5
    while True:
        loop_count += 1
        if time.time() > next_update: # Status ausgeben
            next_update = time.time() + update_duration
            print "loop_count: %s burst: %s load: %ss" % (
                loop_count, burst_count, load
            )

            if loop_count!=target_loop_count:
                new_burst_count = calc_new_count(burst_count, loop_count, target_loop_count)
                if new_burst_count != burst_count:
                    burst_count = new_burst_count
                    print "*** Setzte burst auf: %s" % burst_count

            loop_count = 0
            if not input_thread.is_alive():
                return
            if input_thread.load != load:
                load = input_thread.load
                print "Setzte load auf:", load

        for __ in xrange(burst_count):
            time.sleep(load) # Simulierte Arbeit


regelkreis()
Ziel ist es, das if time.time() > next_update: regelmäßig aufgerufen wird, aber nur so oft, das update_duration eingehalten wird.
Also soll die for __ in xrange(burst_count): Schleife möglichst lange laufen.

Wählt man also eine Statische burst_count Zahl, dann verschenkt man vielleicht zu viel Zeit oder update_duration kann nicht mehr eingehalten werden und erhält somit verzögerte Status Ausgaben.

Also muß man burst_count Zahl dynamisch, je nach "Last" anpassen.
Damit man das sehen kann, habe ich hier eine "Dynamische" Last simuliert. Man kann "+" oder "-" drücken um die Last zu verändern.

Hab versucht möglichst Sinnvolle Ausgaben zu machen, damit man sich die Regelung "ansehen" kann...

Wer will, kann ja mal spielen... und/oder Verbesserungsvorschläge machen, natürlich ;)

Genutzt habe ich es z.Z. hier: https://github.com/jedie/DragonPy/commi ... 27add17984

Re: Regelkreis...

Verfasst: Mittwoch 16. Juli 2014, 12:46
von jens
In der tat, ich brauche das nicht, geht mit threading.Timer() viel einfacher: https://github.com/jedie/DragonPy/commi ... 89635ddc12 :oops: