'Executing 2 async tasks with while loop

I try to click in specific area while cursor moving , task_0 get executed and print works as well but other tasks not getting executed. I have tried using ensure_future , no errors in code.

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...")


Solution 1:[1]

I used await asyncio.sleep to make sure the function gives time to the other function run the problem was not using 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)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 None