I'm having a hard hard time with Timer function from threading.
Basically, when my program starts, I want to log stats every x second.
So I thought I could do it with the Timer function (launch function every 5 second).
For now, I did :
from threading import Timer
def startLogger():
while True:
t = Timer(5, function)
t.start()
def function():
print("hey")
But it launch error, so I think it's not the good way to do it.
RuntimeError: can't start new thread
If someone can give me a clue, it would be appreciated!
Instead of starting a new thread every five seconds, you can create just one thread and run all your code from there.
from time import sleep
from threading import Thread
def startLogger():
while True:
function()
sleep(5)
def function():
print("hey")
Thread(target=startLogger).start()
startLogger will continually run. It'll call function, then pause for 5 seconds, then start again, calling function and so on.
It goes in its own thread so that the sleep(5) doesn't also stop your main thread for 5 seconds.
You can try the following. The idea is, that you are scheduling the next function call just at the end of this function's body:
import threading
def mylog():
print "hey"
` threading.Timer(5.0, mylog)`.start()
mylog()
Related
When using the schedule package in Python, I would like to schedule a task to start at a specific time, and then run every 10 seconds. I was able to get the task to run every 10 seconds, using schedule.every(10).seconds.do(x) and I also got it to run at a set time, using schedule.every().day.at('13:25').do(x). But how would I put these together? I tried to combine them into the following, but I got RecursionError: maximum recursion depth exceeded
import schedule
import time
def test():
print('Hello, World!')
def sched_job():
schedule.every(10).seconds.do(test)
while True:
schedule.run_pending()
time.sleep(1)
schedule.every().day.at('13:56').do(sched_job)
while True:
schedule.run_pending()
time.sleep(1)
sched_job()
Don't call run_pending() from inside your job, just schedule an additional job and use your main loop to call it. The only thing you need to change in your code is removing the while True block in sched_job(). Also, in order to prevent a second schedule to be created every 10 seconds on the next day at the given time, the outer job should immediately cancel itself once it is executed once. You can do this by returning schedule.CancelJob.
Here is the modified code:
import schedule
import time
def test():
print('Hello, World!')
def sched_job():
schedule.every(10).seconds.do(test)
return schedule.CancelJob
schedule.every().day.at('13:56').do(sched_job)
while True:
schedule.run_pending()
time.sleep(1)
I have a thread, where I am using an thread event to control the thread from outside the target function.
flag = threading.event()
In my target function, I have something like this:
def functionname(arguments):
flag.clear()
while not flag.wait(timeout = 0.5):
# do something.
whenever I want to return the thread, from my main function I say, flag.set(). Then flag is set to true, my target function completes execution and the thread is completed.
Now, if I want to use flag.wait(timeout = 5) in my main function, I am expecting to block and wait for five seconds to execute the "do something" part of code. However I am seeing that the "do something" part of the code is executing every 0.5 seconds as usual and my main function is blocked for five seconds.
The wait method has to block until the flag is true or the optional time out ends. It is blocking my main function rather than the target function. Can any one know why this might be?
PS: I defined the flag event in my main function and passed it as an argument to the target function
Many threads can wait on an event at the same time and how long one waits before timeout is independent of how long the others do. If you want your thread's wait time to change from .5 to 5 seconds, then you need some way to tell the thread to change the value it passes in the timeout parameter. This seems like a good job for a shared variable - the thread reads timeout from the variable and main can change that variable.
But where do you put it? Well, class instances exist to hold associated data so a class holding the event and the current timeout value is reasonable. In fact, you can just use inheritance to do the job. Notice in this example, the timeout is only changed for the next interval - the thread waits the current wait time before using the updated value. Getting the change immediately is significantly more difficult and I didn't want to confuse the matter.
import threading
import time
class VariablyTimedEvent(threading.Event):
def __init__(self, initial_timeout=None):
super().__init__()
self.timeout = initial_timeout
def wait(self):
return super().wait(timeout=self.timeout)
# ------------- test -----------------
def functionname(event):
event.clear()
while not event.wait():
do_something()
print('event set, exiting')
_do_something_start = time.time()
def do_something():
# a mock object that shows time since program start
print('%03.1f: do something' % (time.time() - _do_something_start))
print("start timing at .5 second")
event = VariablyTimedEvent(.5)
thread = threading.Thread(target=functionname, args=(event,))
thread.start()
time.sleep(3.1)
print("change timeout to 2 seconds")
event.timeout = 2
time.sleep(7)
print("exit the thread")
event.set()
thread.join()
I am trying to run a specific function in my python file. However, when I run the method with the timer that calls said function, it executes everything that it's supposed to, but then exits the job after the first time. I need it to continue to run the function after the specified time.
This is the function that contains the timer:
def executor(file):
x = datetime.today()
y = x.replace(day=x.day, hour=x.hour, minute=x.minute, second=x.second+10, microsecond=0)
delta_t = y-x
secs = delta_t.seconds+1
t = Timer(secs, parse_file, [file])
t.start()
The function that I am trying to call, is parse_file(file_name).
I am passing in the file_name when calling the executor function.
You haven't given enough detail of what your actual issue is, what code do you want to run more than once? Can you show the code that actually calls this function?
When you call start, the main thread will continue executing from that spot, while the task you scheduled will call the parse_file method at the specified time, and exit once complete. It sounds to me like you don't have anything that is keeping your main thread alive (that is, you don't have any more code after you call the executor).
Here is a small example showing how you can use the Timer to execute tasks while the main thread is still working. You can keep typing in input, and the print statement will show you all the threads that completed since the last time you typed an input.
from threading import Timer
import sys
def scheduled_task(arg):
print("\ntask complete arg %s!\n"%(arg))
def run_scheduled_task(arg):
timer = Timer(10, scheduled_task, [arg])
timer.start()
done = False
while not done:
user_input = input("Give me some input (exit to stop): ")
if user_input == 'exit':
print('Exiting')
done = True
else:
run_scheduled_task(user_input)
I am somewhat new to python. I have been trying to find the answer to this coding question for some time. I have a function set up to run on a threading timer. This allows it to execute every second while my other code is running. I would like this function to simply execute continuously, that is every time it is done executing it starts over, rather than on a timer. The reason for this is that due to a changing delay in a stepper motor the function takes different amounts of time run.
Is this what you're looking for?
from threading import Thread
def f():
print('hello')
while True:
t = Thread(target=f)
t.start()
t.join()
Or maybe this, which shows the concurrent paths of execution (for production, remove sleep() calls, of course):
from threading import Thread
from time import sleep
def g():
print('hello')
sleep(1)
def f():
while True: g()
Thread(target=f).start()
sleep(1)
print('other code here')
I want to execute a function every 60 seconds on Python but I don't want to be blocked meanwhile.
How can I do it asynchronously?
import threading
import time
def f():
print("hello world")
threading.Timer(3, f).start()
if __name__ == '__main__':
f()
time.sleep(20)
With this code, the function f is executed every 3 seconds within the 20 seconds time.time.
At the end it gives an error and I think that it is because the threading.timer has not been canceled.
How can I cancel it?
You could try the threading.Timer class: http://docs.python.org/library/threading.html#timer-objects.
import threading
def f(f_stop):
# do something here ...
if not f_stop.is_set():
# call f() again in 60 seconds
threading.Timer(60, f, [f_stop]).start()
f_stop = threading.Event()
# start calling f now and every 60 sec thereafter
f(f_stop)
# stop the thread when needed
#f_stop.set()
The simplest way is to create a background thread that runs something every 60 seconds. A trivial implementation is:
import time
from threading import Thread
class BackgroundTimer(Thread):
def run(self):
while 1:
time.sleep(60)
# do something
# ... SNIP ...
# Inside your main thread
# ... SNIP ...
timer = BackgroundTimer()
timer.start()
Obviously, if the "do something" takes a long time, then you'll need to accommodate for it in your sleep statement. But, 60 seconds serves as a good approximation.
I googled around and found the Python circuits Framework, which makes it possible to wait
for a particular event.
The .callEvent(self, event, *channels) method of circuits contains a fire and suspend-until-response functionality, the documentation says:
Fire the given event to the specified channels and suspend execution
until it has been dispatched. This method may only be invoked as
argument to a yield on the top execution level of a handler (e.g.
"yield self.callEvent(event)"). It effectively creates and returns
a generator that will be invoked by the main loop until the event has
been dispatched (see :func:circuits.core.handlers.handler).
I hope you find it as useful as I do :)
./regards
It depends on what you actually want to do in the mean time. Threads are the most general and least preferred way of doing it; you should be aware of the issues with threading when you use it: not all (non-Python) code allows access from multiple threads simultaneously, communication between threads should be done using thread-safe datastructures like Queue.Queue, you won't be able to interrupt the thread from outside it, and terminating the program while the thread is still running can lead to a hung interpreter or spurious tracebacks.
Often there's an easier way. If you're doing this in a GUI program, use the GUI library's timer or event functionality. All GUIs have this. Likewise, if you're using another event system, like Twisted or another server-process model, you should be able to hook into the main event loop to cause it to call your function regularly. The non-threading approaches do cause your program to be blocked while the function is pending, but not between functioncalls.
Why dont you create a dedicated thread, in which you put a simple sleeping loop:
#!/usr/bin/env python
import time
while True:
# Your code here
time.sleep(60)
I think the right way to run a thread repeatedly is the next:
import threading
import time
def f():
print("hello world") # your code here
myThread.run()
if __name__ == '__main__':
myThread = threading.Timer(3, f) # timer is set to 3 seconds
myThread.start()
time.sleep(10) # it can be loop or other time consuming code here
if myThread.is_alive():
myThread.cancel()
With this code, the function f is executed every 3 seconds within the 10 seconds time.sleep(10). At the end running of thread is canceled.
If you want to invoke the method "on the clock" (e.g. every hour on the hour), you can integrate the following idea with whichever threading mechanism you choose:
import time
def wait(n):
'''Wait until the next increment of n seconds'''
x = time.time()
time.sleep(n-(x%n))
print(time.asctime())
[snip. removed non async version]
To use asyncing you would use trio. I recommend trio to everyone who asks about async python. It is much easier to work with especially sockets. With sockets I have a nursery with 1 read and 1 write function and the write function writes data from an deque where it is placed by the read function; and waiting to be sent. The following app works by using trio.run(function,parameters) and then opening an nursery where the program functions in loops with an await trio.sleep(60) between each loop to give the rest of the app a chance to run. This will run the program in a single processes but your machine can handle 1500 TCP connections insead of just 255 with the non async method.
I have not yet mastered the cancellation statements but I put at move_on_after(70) which is means the code will wait 10 seconds longer than to execute a 60 second sleep before moving on to the next loop.
import trio
async def execTimer():
'''This function gets executed in a nursery simultaneously with the rest of the program'''
while True:
trio.move_on_after(70):
await trio.sleep(60)
print('60 Second Loop')
async def OneTime_OneMinute():
'''This functions gets run by trio.run to start the entire program'''
with trio.open_nursery() as nursery:
nursery.start_soon(execTimer)
nursery.start_soon(print,'do the rest of the program simultaneously')
def start():
'''You many have only one trio.run in the entire application'''
trio.run(OneTime_OneMinute)
if __name__ == '__main__':
start()
This will run any number of functions simultaneously in the nursery. You can use any of the cancellable statements for checkpoints where the rest of the program gets to continue running. All trio statements are checkpoints so use them a lot. I did not test this app; so if there are any questions just ask.
As you can see trio is the champion of easy-to-use functionality. It is based on using functions instead of objects but you can use objects if you wish.
Read more at:
[1]: https://trio.readthedocs.io/en/stable/reference-core.html