当光标移动时,我尝试点击特定的区域,task_0被执行,打印也工作,但是其他的任务没有被执行。我试过使用ensure_future,代码中没有错误。
import pyautogui
import time
import asyncio
async def main():
print(pyautogui.size())
task_0 = asyncio.create_task(cursor_track())
print("before minimize...")
task_1 = asyncio.create_task(minimize_click())
task_2 = asyncio.create_task(will_it_run())
async def will_it_run():
print("running!...")
# Click on minimize
async def minimize_click():
print("\nTest...\n")
x, y = pyautogui.position()
while True:
if (1780 <= x <= 1820) and (0 <= y <= 25):
pyautogui.click(clicks=1)
# Prints Cursor position
async def cursor_track():
print('Press Ctrl-C to quit.')
try:
while True:
x, y = pyautogui.position()
positionStr = 'X: ' + str(x).rjust(4) + ' Y: ' + str(y).rjust(4)
print(positionStr, end='')
time.sleep(0.1)
print('\b' * len(positionStr), end='', flush=True)
except KeyboardInterrupt:
print('\n')
asyncio.run(main())
print("async test finished...")发布于 2022-03-10 08:09:08
我使用await asyncio.sleep来确保函数给其他函数时间,运行问题是没有使用await
import pyautogui
import time
import asyncio
async def track_and_minimze():
print("\n"+str(pyautogui.size())+"\n")
task_0 = asyncio.create_task(cursor_track())
task_1 = asyncio.create_task(minimize_click())
await task_0
await task_1
# Click on minimize
async def minimize_click():
while True:
x, y = pyautogui.position()
await asyncio.sleep(0.01)
if (1780 <= x <= 1825) and (0 <= y <= 25):
pyautogui.click(clicks=1)
# Prints Cursor position
async def cursor_track():
try:
while True:
x, y = pyautogui.position()
positionStr = 'X: ' + str(x).rjust(4) + ' Y: ' + str(y).rjust(4)
await asyncio.sleep(0.01)
print(positionStr, end='')
time.sleep(0.05)
print('\b' * len(positionStr), end='', flush=True)https://stackoverflow.com/questions/71403907
复制相似问题