![]() ![]() If you tested positive for COVID-19 or are waiting for test results (regardless of vaccination status, whether symptomatic or asymptomatic): While mutable_.value or (time.time() - start <=. T = Thread(target=myfunc, args=(mutable_.49)) Here's a v2 using thread sleeping to get most of the way there (that 0.49) and a, normally short-circuited time.time() check to finalize. Might be possible to set your thread time out at 299.9 seconds, get close and finish more carefully. Now, in my test, there was a bit of a difference where the thread had not yet updated the value so the loop ran a bit over compared to using own local time check. The unknown here to me is whether the loop's attribute access to mutable_.value, which doesn't need to involve the thread directly is that much cheaper than polling the thread itself. T = Thread(target=myfunc, args=(mutable_,)) time.time() itself is probably already fairly highly optimized, so all the extra booleans and stuff to avoid using it too much probably don't improve things much).ĭon't know if it's more efficient, but you could try a thread and a shared mutable object to drive the loop. ![]() I get a significant speed up from just breaking after a certain number of iterations, but whether using a thread, time.time directly or a partial sampling approach, pretty much all of them end up degrading loop time. (a bit of tinkering later, I think I broadly agree with Alain. In my tests the two approaches varied by less than 0.5 second over a 60 second sample executing 41,000 iterations: +0.37sec for time() and +0.23sec for thread. ![]() Either that or you are checking for timeout a lot more often than the 11,311 iterations would suggest (perhaps by checking inside a nested loop). running the same number of iterations in a loop will vary from one run to the next). If you are getting a 3 seconds difference over a 1 minute sample, I would suspect that the processing time of your logic has its own variability (i.e. You can do it in a simple way using a lambda function to create a sleeping background thread and simply check if if the thread is still running inside your loop (break the loop when time is up): from threading import ThreadįiveMinutes = Thread(target=lambda:sleep(300)) # 5 min sleeping thread (300 seconds)įor i in range(10**10): # fiveMinutes : break # break after 5 minutes elapsed I think using threading and sleep is the best approach. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |