I am using X11 display server. I have a Python script that uses PyAutoGUI to turn touches on the screen into mouse movement and controls. The packets which send the coordinate (and other) data of touches is sent over a named pipe (fifo). Because of this, I need to run a PyAutoGUI function within a thread that checks if the last packet has timed out and so the mouse should lift up (this is to determine between a tap and a drag and also end a drag).
For the program to run smoothly, I need to set:
pyautogui.PAUSE = 0
However, this seemingly causes a race condition between pyautogui and the X11 server and so the program crashes when it receives a reply that it was not expecting:
RuntimeError: Expected reply for request 833, but got 834. Can't happen!
Here is the relevant code:
import time
import threading
import Xlib.threaded # this is to make the Xlib dependency of pyautogui threadsafe
import pyautogui as pag
pag.FAILSAFE= False
pag.PAUSE = 0
PIPE_PATH = "my path to named pipe"
screenWidth, screenHeight = pag.size()
global first_touch
first_touch = True
global previous_time
previous_time = None
def touch_timeout() -> None:
global first_touch
global previous_time
while True:
if previous_time != None:
while True:
local_time = time.time()
if local_time - previous_time > 0.01:
pag.mouseUp() # error appears here
first_Touch = True
timeout_thread = threading.Thread(target=touch_timeout)
timeout_thread.start()
with open(PIPE_PATH, "r") as f:
while True:
for message in f:
previous_time = time.time()
# packet handling logic here ...
# ...
pag.moveTo(xcoord, ycoord)
if first_touch:
pag.mouseDown()
first_Touch = False
Here is what I have tried:
I have disabled the thread (which removes the functionality of the mouse moving up but this is okay for this test). This resulted in no error and smooth performance.
I have adjusted the pag.PAUSE to various values. Anything other than 0 is too slow and does not provide required performance. However, the higher the value, the less often the mentioned error occurs.