Python Forum
Hourly action resetter with threading?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Hourly action resetter with threading?
#1
This is some code I have written based off basic knowledge of threading I acquired. What I am trying to do is limit the actions hourly so it doesn't spam:
import datetime
import time
from threading import Thread

the_Minute   = str(datetime.datetime.now().time())[3:5]
action_Limit = 0

def updater():
    global the_Minute
    while True:
        the_Minute=str(datetime.datetime.now().time())[3:5]
        #updates the minute value

def resetter():
    global action_Limit
    global the_Minute
    while True:
        if the_Minute=='00':
            action_Limit=0
        time.sleep(30)


def performer():
    global action_Limit
    global the_Minute
    while the_Minute!='00' and action_Limit<100:
        #perform actions here
        action_Limit+=1
        print(action_Limit)
        time.sleep(1)

updater_Thread   = Thread(target=updater)
resetter_Thread  = Thread(target=resetter)
performer_Thread = Thread(target=performer)

updater_Thread.start
performer_Thread.start
resetter_Thread.start 
When I run this, nothing happens, but I also don't receive any errors. Could anyone tell me how I could make it work, point me to some resources to help, or notify me of any better/other ways to do this? Thank you for your time.
Reply
#2
I don't understand what this code is supposed to do, but for sure nothing will happen if you dont start a thread by calling the start() method, for example
updater_Thread.start()  # <-- see the parentheses?
The main issue with this code is that you will have 3 threads changing the values of the same two variables, which is close to three persons eating in the same plate. You'll probably need a Lock object very soon.
Reply
#3
(Jan-09-2018, 11:32 PM)Gribouillis Wrote: I don't understand what this code is supposed to do
the variable the_Minute is set to the current minute in the hour, and the updater function is reevaluating the_Minute so it displays the updated version of the time, the performer function is an example function that in practice should be running lines of code, which I want to limit its running to 100 times per hour, and the resetter function is resetting the variable that limits the performer's running every hour. I hope that helps! Also thank you for fixing my small issue with the parentheses, that always happens. I don't think multiple threads changing the value of the variable will be a problem in this case, as they do it in different time frames. The performer is expected to finish the task before the hour, which is when the resetter function modifies this variable. Nothing detrimental happens even when I intentionally start the performer function right before the resetter activates, so that the two kind of overlap. Thank you for helping!
Reply
#4
You can do the same thing with a single Timer thread. Here is a code, where I changed 100 times every hour by 10 times every minute, but it is easy to adapt to hours by changing lines 4 and 12
import datetime as dt
import threading
import time
reset_delta = dt.timedelta(minutes=1)

action_limit = None
timer = None

def delta_until_reset():
    now = dt.datetime.now()
    t = now + reset_delta
    t = dt.datetime(t.year, t.month, t.day, t.hour, t.minute)
    return max(t - now, dt.timedelta())

def reset():
    global action_limit, timer
    action_limit = 0
    secs = delta_until_reset().total_seconds()
    print('secs: ', secs)
    timer = threading.Timer(secs, reset)
    timer.start()

def performer(action_max):
    global action_limit
    while action_limit < action_max:
        action_limit += 1
        print(action_limit)
        time.sleep(1)

if __name__ == '__main__':
    reset()
    while True:
        performer(10)
        time.sleep(0.5)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Concurrent futures threading running at same speed as non-threading billykid999 13 1,798 May-03-2023, 08:22 AM
Last Post: billykid999
  Pixel color and action Sartre 4 2,054 Apr-13-2023, 03:26 AM
Last Post: Sartre
Question Running an action only between certain times alexbca 9 1,707 Mar-15-2023, 04:21 PM
Last Post: deanhystad
  Checkbox itens with a button to run action Woogmoog 3 944 Dec-19-2022, 11:54 AM
Last Post: Woogmoog
Question Running an action only if time condition is met alexbca 5 1,305 Oct-27-2022, 02:15 PM
Last Post: alexbca
  Tutorials on sockets, threading and multi-threading? muzikman 2 2,113 Oct-01-2021, 08:32 PM
Last Post: muzikman
  Getting the hourly average of a time series dataset Raskou07 10 12,496 Dec-15-2020, 12:51 PM
Last Post: palladium
  Parsing Date/Time from Metar Reports with 6 hourly weather information Lawrence 0 2,322 May-03-2020, 08:15 PM
Last Post: Lawrence
  Have an amount of time to perform and action CookieGamez2018 1 2,939 Dec-21-2018, 07:12 AM
Last Post: Gribouillis
  action on MQTT while long loop is running runboy 4 6,062 Oct-05-2018, 11:57 PM
Last Post: runboy

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020