Search code examples
python-3.xspyderpyautogui

Python: How to run some specific code snippet for 10 minutes from current time?


CODE

from random import *
import pyautogui, datetime, sys, time 

ts = time.strftime('%X') #saving current time in ts
scroll_up = pyautogui.scroll(50) #scroll up 50 "clicks"

I want this code to run 10 minutes from current time.

Suppose current time in ts is -> 13:02:08 and I want to run that code till 13:12:08 and after 10 minutes the code stops.

How can I do that?

I'm trying to find the difference between human and computer behavior (pattern).


Solution

  • You can use a while loop and check the time delta (change)

    from datetime import datetime
    
    start_time = datetime.now()
    
    while True:
    
        #  To something
    
        current_time = datetime.now()
        duration = current_time - start_time
        duration_minutes = ((duration.seconds % 3600) / 60) 
    
        if duration_minutes > 10:
            break # exit loop