Python:如何 运行 从当前时间起 10 分钟内的某些特定代码片段?

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

代码

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"

我希望此代码从当前时间开始 运行 10 分钟。

假设当前时间在 ts is -> 13:02:08,我想 运行 该代码直到 13:12:08,10 分钟后代码停止。

我该怎么做?

我正在尝试找出人类和计算机行为(模式)之间的区别。

您可以使用 while 循环并检查时间增量(更改)

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