it is necessary for the program to stop the while loop at exactly the appointed time, now I do this:

import datetime import time import requests while datetime.datetime.now().strftime('%H%M%S') != '143400': requests.get('url') time.sleep(10) #Выполнение операции print в 14:34:00 print('End') 

But the problem is that if time.sleep (10) is called, for example, at 14:33:59 (later than 10 seconds before the end) or the request does not have time, the cycle will become infinite. Tell me, please, how to make sure that any operation stops at the appointed time. The site to which requests are submitted is not rarely overloaded, and because of this, it takes requests for a long time.

    2 answers 2

    Check the exact time, and the interval:

     t = int (datetime.datetime.now().strftime('%H%M%S')) while t < 143350 or t > 143420: # здесь код t = int (datetime.datetime.now().strftime('%H%M%S')) 

    If the current time is between 14:33:50 and 14:34:20, then the cycle ends.

    • It would then be possible to use datetime.time - andreymal

    it is necessary for the program to stop the while loop at exactly the appointed time

    I strongly suspect that in fact, you need something completely different. Namely:

    The program must start the execution of some action exactly at the time

    So? Then you need to use the standard function:

    signal.alarm (time)

     If time is non-zero, this function requests that a SIGALRM signal be sent to the process in time seconds. Any previously scheduled alarm 

    is canceled (only one alarm can be scheduled at any time). If you’re previously set If the alarm is canceled, no alarm is canceled. If the return value is zero, no alarm is currently scheduled. (See the Unix man page alarm (2).) Availability: Unix.

    Of course, before this call, you need to install a signal handler SIGALRM, which will trigger the desired action.

    • "Availability: Unix". The solution is not available for all systems. - Enikeyschik February