S
S
s4q2020-06-16 22:35:11
Python
s4q, 2020-06-16 22:35:11

How to stop the execution of the loop after a certain time without interfering with the code that is being executed in the loop?

The loop performs a certain function, and after a while it must finish executing the function, and the loop itself.
Here is the loop itself:

for i in range(100):
  new_people_and_actions()

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
nitr0_313, 2020-06-17
@s4q

Maybe, if we are talking about time, then do this:

import time
stop = time.time() + 10 # сколько секунд будет выполняться функция

while stop > time.time():
    new_people_and_actions()

S
Saboteur, 2020-06-16
@saboteur_kiev

add an external marker, such as a file, a flag, send a signal.
Add its handling inside the loop to complete correctly.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question