python – Selenium Web Driver. Problem with simultaneous requests

The essence of the program is that it receives an HTTP request with query parameters such as “url” and “canCache”.
Then the program has to open a new tab, go to the url and take a screenshot of the page and then close the tab. Problem: with simultaneous queries, the results are either mixed or result in an error.

Here are my 2 pieces of my code.

#schedule_gen.py

import urllib
from io import BytesIO
from cache import cache_schedule
import logging


def schedule_png_gen(url, can_cache, driver):
    original_window = driver.current_window_handle

    # Setting up Selenium Web Driver.
    # Running in windowless mode, configuring the browser type (Firefox), getting the link
    driver.switch_to.new_window('tab')
    driver.get(url)

    # Added a script that measures page height and width.
    # Made to avoid the error when the 'body' tag ends where it shouldn't.
    required_width = driver.execute_script('return document.body.parentNode.scrollWidth')
    required_height = driver.execute_script('return document.body.parentNode.scrollHeight')
    driver.set_window_size(required_width, required_height)

    # Screenshot of the page, adding the .png extension and encoding special characters
    screen = driver.get_screenshot_as_png()
    screen_buffer = BytesIO(screen)
    screen_size = screen_buffer.getbuffer().nbytes
    url_upd1 = url + '.png'
    url_upd2 = urllib.parse.quote(url_upd1, safe="")

    # If you want to cache the page, call the cache schedule method.
    # The input parameters of the method are described in cache.py
    if can_cache:
        cache_schedule(url_upd2, screen_buffer, screen_size)
    # Resetting the window size to avoid taking the screenshot of the previous page's size.
    driver.set_window_size(1920, 1080)

    # Closing the current tab.
    driver.close()

    # Switching back to the original tab.
    driver.switch_to.window(driver.window_handles[0])
    return screen

#main.py

from flask import Flask, Response, request
from cache import get_cached_schedule
from schedule_gen import schedule_png_gen
from selenium import webdriver
from selenium.webdriver.firefox.options import Options

options = Options()
options.add_argument('--headless')
driver = webdriver.Firefox(options=options)

# Running the Flask application.
app = Flask(__name__)

# Request handling.
@app.route('/schedule', methods=['GET'])
def schedule():
    # Processing query parameters.
    url = request.args.get('url')
    can_cache = request.args.get('canCache', False)

    if url == None:
        return ("No url")

    # Checking for the existence of a screenshot in the Minio database.
    image_data = get_cached_schedule(url)
    if image_data:
        return Response(image_data, mimetype="image/png")
    else:
         screen = schedule_png_gen(url, can_cache, driver)
         return Response(screen, mimetype="image/png")


if __name__ == '__main__':
    app.run(debug=True, port=8800, host="0.0.0.0")

Initially I tried adding a time delay, thinking that the mixing is caused by different loading speeds of the sites, but it did not give any results

Read more here: Source link