Time-Limited Input? [duplicate]

If it is acceptable to block the main thread when user haven’t provided an answer: from threading import Timer timeout = 10 t = Timer(timeout, print, [‘Sorry, times up’]) t.start() prompt = “You have %d seconds to choose the correct answer…\n” % timeout answer = input(prompt) t.cancel() Otherwise, you could use @Alex Martelli’s answer (modified … Read more

Resettable Java Timer

According to the Timer documentation, in Java 1.5 onwards, you should prefer the ScheduledThreadPoolExecutor instead. (You may like to create this executor using Executors.newSingleThreadScheduledExecutor() for ease of use; it creates something much like a Timer.) The cool thing is, when you schedule a task (by calling schedule()), it returns a ScheduledFuture object. You can use … Read more

How do you time a function in Go and return its runtime in milliseconds?

Go’s defer makes this trivial. In Go 1.x, define the following functions: func trace(s string) (string, time.Time) { log.Println(“START:”, s) return s, time.Now() } func un(s string, startTime time.Time) { endTime := time.Now() log.Println(” END:”, s, “ElapsedTime in seconds:”, endTime.Sub(startTime)) } After that, you get Squeaky Clean one line elapsed time log messages: func someFunction() … Read more

VBA Macro On Timer style to run code every set number of seconds, i.e. 120 seconds

When the workbook first opens, execute this code: alertTime = Now + TimeValue(“00:02:00”) Application.OnTime alertTime, “EventMacro” Then just have a macro in the workbook called “EventMacro” that will repeat it. Public Sub EventMacro() ‘… Execute your actions here’ alertTime = Now + TimeValue(“00:02:00”) Application.OnTime alertTime, “EventMacro” End Sub