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

Go To StackoverFlow.com

0

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
by TLSK
Timer is also an option, see http://stackoverflow.com/questions/9999873/python-watch-long-running-process-alert-after-so-many-minutes/9999994#999999 - Niklas B. 2012-04-03 22:39


0

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

from time import sleep

do_task()
sleep(60)
do_task()

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


3

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.

do_task()
wx.CallLater(60 * 1000, do_task)

See http://www.wxpython.org/docs/api/wx.CallLater-class.html

2012-04-08 15:12
by Frank Niessink