A
Can I help you? range(n) use it to repeat something. n once in Pitone:for _ in range(60):
do_something()
time.sleep(1)
Right. and "effective" Is that a way? This depends on a specific task.The code is simple and understandable: do_something() 60 times, each next challenge occurs no earlier than a second after the completion of the previous challenge. At the end, we wait a second before we finish the cycle.Imagine. do_something() requires half a second on average, then the cycle will take a minute and a half or more. If that depends on the task.If you want the challenges to happen at the border of every second:for _ in range(60):
do_something()
time.sleep(1 - time.time() % 1)
Precision before other processes, flows (specificity of process/flow planners in the operating system, GIL features of the Pitton Interpreter version), each challenge do_something() Except the first one comes close to time when time.time() the whole thing is returning.For example, if time.time() Come back. X.3 seconds. time.sleep() at least to sleep. 0.7 seconds (if the signal processor doesn't throw off) and means the next challenge. do_something() It's going to happen. X + 1 For a second in system time (if it didn't jump much while we were asleep). In this case, challenges do_something() may be more uniformly distributed and the cycle ends almost immediately in a minute, according to time.time() (if the time of implementation do_something() less than a second). If that depends on the task.If you don't want to start do_something()If more than a minute has passed in accordance with the chosen time limit, it is possible to use a clear condition:deadline = time.monotonic() + 60
while time.monotonic() < deadline:
do_something()
time.sleep(1)
Items to be taken into account depending on the task:What a desirable attitude, if do_something() May be carried out more than a second: skip the irration, start a separate flow, flow pool/process?What's the desired behavior if the system time jumps (because someone shuts the laptop cover during the cycle or https://ru.wikipedia.org/wiki/%D0%93%D0%B8%D0%B1%D0%B5%D1%80%D0%BD%D0%B0%D1%86%D0%B8%D1%8F_%28%D0%BE%D0%BF%D0%B5%D1%80%D0%B0%D1%86%D0%B8%D0%BE%D0%BD%D0%BD%D1%8B%D0%B5_%D1%81%D0%B8%D1%81%D1%82%D0%B5%D0%BC%D1%8B%29 For any other reason, or the mobile OS has downloaded your process to save energy in a while, or the code inside the VM, the big jumps are possible. Do you want to continue the cycle as if nothing happened when the system wakes up, or to cancel the subsequent challenges (outstandings) or to meet the remaining challenges as soon as possible, without pause or at all (deadline passed)?For example, if time changes during performance https://ru.stackoverflow.com/a/577286/23044 What do you think will happen? Whether this conduct is documented sched Module (is it not going to change between the different implementations/versions of Piton)? Is this important in your particular case? sleep() may become particularly attractive at this point.As a variation on the topic, https://stackoverflow.com/a/22498708/4279 or https://stackoverflow.com/a/14040516/4279 ♪ In more complex cases, if you don't have special preferences, you can. https://apscheduler.readthedocs.io ♪ In some cases, it makes sense to use the system planner (for large intervals) such as cron and Windows Task Scheduler.It is useful to know why call_repeatedly() removed https://www.python.org/dev/peps/pep-3156/#basic-callbacks :Note: A previous version of this PEP defined a method named
call_repeatedly() , which promised to call a callback at regular
intervals. This has been withdrawn because the design of such a
function is overspecified. On the one hand, a simple timer loop can
easily be emulated using a callback that reschedules itself using
call_later() ; it is also easy to write coroutine containing a loop
and a sleep() call (a toplevel function in the module, see below). On
the other hand, due to the complexities of accurate timekeeping there
are many traps and pitfalls here for the unaware ( https://www.python.org/dev/peps/pep-0418/ ), and
different use cases require different behavior in edge cases. It is
impossible to offer an API for this purpose that is bullet-proof in
all cases, so it is deemed better to let application designers decide
for themselves what kind of timer loop to implement.