Windows 10系统下Python鼠标模拟脚本运行异常缓慢问题咨询
排查步骤与修复方案
禁用pyautogui不必要的默认检查
pyautogui默认开启FAILSAFE故障安全机制,每次执行鼠标操作前都会检查鼠标是否位于屏幕左上角触发中断,在Windows10多屏、高DPI设备上该检查会产生额外开销,可在代码开头加入py.FAILSAFE = False关闭。同时关闭默认的操作间隔校验,添加py.MINIMUM_DURATION = 0避免系统强制增加操作延迟。调整Windows10鼠标系统设置
打开Windows10「设置」-「蓝牙和其他设备」-「鼠标」-「其他鼠标设置」,在指针选项卡中取消勾选提高指针精确度,该功能会对程序发起的鼠标移动请求做平滑插值处理,直接导致批量移动操作耗时翻倍。修正短耗时下的定时器精度问题
Windows10默认系统定时器精度为15.6ms,若HumanCurve生成的移动点数量过多,你设置的py.PAUSE = 0.1 / 点数会小于系统最小调度粒度,实际等待时间会被系统强制拉长。可修改逻辑:若单步间隔小于10ms,直接关闭单步暂停,改为一次性控制总移动时长:
import pyautogui as py import time import ctypes from pyclick.humancurve import HumanCurve class HumanClicker(): def __init__(self): # 开启进程高DPI感知,避免坐标转换开销 ctypes.windll.shcore.SetProcessDpiAwareness(2) py.FAILSAFE = False py.MINIMUM_DURATION = 0 def move(self, fromPoint, toPoint, duration=2, humanCurve=None): if not humanCurve: humanCurve = HumanCurve(fromPoint, toPoint) points = humanCurve.points if duration <= 0.2: # 短耗时场景跳过逐帧等待,直接控制总时长 start = time.time() for point in points: py.moveTo(point, duration=0) # 补全剩余耗时 end = time.time() if end - start < duration: time.sleep(duration - (end - start)) else: py.PAUSE = duration / len(points) for point in points: py.moveTo(point) hc = HumanClicker() hc.move((100, 100), (150, 150), 0.1)
内容的提问来源于stack exchange,提问作者James
相关产品推荐
相关产品推荐

