How to run a task in Python after 60 second but only one time

Go To


I have a task in Python and i run this task for first time and i want after 60 second run the same task but only one last time.

2012-04-03 22:26
Timer is also an option, see - Niklas B. 2012-04-03 22:39


If you want something done twice with a gap, the simplest way is just to sleep for a bit:

from time import sleep


Note that this could not be suitable if you need precisely 60 seconds.

The actual suspension time may be less than that requested because any caught signal will terminate the sleep() following execution of that signal’s catching routine. Also, the suspension time may be longer than requested by an arbitrary amount because of the scheduling of other activity in the system.

In that case, you may want to store the time before the sleep and compare to ensure the right amount of time has passed, for example, but in most cases it doesn't need to be so precise.

Note that this pauses execution, if you want to do this with other things going on, you will need to look into threading.

2012-04-03 22:34
by Gareth Latty
Note that the OP added the wxPython tag, so presumably this is a GUI application. Calling sleep would render the user interface frozen - Frank Niessink 2012-04-08 15:06
I missed that tag, fair enough - Gareth Latty 2012-04-08 18:57


Call the task directly the first time and from a timer the second time. The call to wx.CallLater will not block, so the application remains responsive while the timer is running.

wx.CallLater(60 * 1000, do_task)


2012-04-08 15:12
by Frank Niessink