I'm trying to adapt this module to support asynchronous execution when searching for a lot of images in the same screenshot at a given time. I'm kind of new to async coding and after a lot of research I chose Trio to do it (because of it's awesomeness and ease).
The point is:
- The function receives a list of paths of images
- At each iteration, it takes a screenshot and tries to find the images in the array (it's better for performance if we don't take a new screenshot for every try in the array)
- If it finds one, returns the image's path and it's coordinates
- Do it all over again because some image may appear now on the screen
I'm going to use this in another project with support for async with Trio, that's why I'm trying to convert it.
This is my attempt:
def image_search(image, precision=0.8, pil=None):
if pil is None:
pil = pyautogui.screenshot()
if is_retina:
pil.thumbnail((round(pil.size[0] * 0.5), round(pil.size[1] * 0.5)))
return most_probable_location(pil, image, precision)
async def multiple_image_search_loop(images, interval=0.1, timeout=None, precision=0.8):
async def do_search():
while True:
pil = pyautogui.screenshot()
for image in images:
if pos := image_search(image, precision, pil):
return {
"position": pos,
"image": image
}
await trio.sleep(interval)
if timeout:
with trio.fail_after(timeout):
return await do_search()
else:
return await do_search()
Although the code looks correct I feel I'm missing the point of asynchronous code. This could all be done in a synchronous manner, and I feel I haven't made any difference in it.
It's not so bad if there is no difference in performance because the point is to make this function useful in an async context, without blocking for the whole time it is searching for the images, but if I could optimize things, It sure would be better.
Maybe if instead of awaiting
after a search on all images I adapt image_search()
with a call to trio.sleep()
and open a nursery on the main function would be better? (using the trio.start_soon()
method within it for each image on the array). This would block less on the other project I'm going to use it but It would take more time to find an image, am I right?