Skip to content

Instantly share code, notes, and snippets.

@kumaraditya303
Last active June 15, 2024 04:51
Show Gist options
  • Save kumaraditya303/e6dee949dda298b35d167369955d45c6 to your computer and use it in GitHub Desktop.
Save kumaraditya303/e6dee949dda298b35d167369955d45c6 to your computer and use it in GitHub Desktop.
MultiThreaded Playwright with ThreadPoolExecutor
import threading
from playwright.sync_api import sync_playwright
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor
class Tls(threading.local):
def __init__(self) -> None:
self.playwright = sync_playwright().start()
print("Create playwright instance in Thread", threading.current_thread().name)
class Worker:
tls = Tls()
def run(self):
print("Launched worker in ", threading.current_thread().name)
browser = self.tls.playwright.chromium.launch(headless=False)
context = browser.new_context()
page = browser.new_page()
page.goto("http://whatsmyuseragent.org/")
page.screenshot(path=f"example-{threading.current_thread().name}.png")
page.close()
context.close()
browser.close()
print("Stopped worker in ", threading.current_thread().name)
if __name__ == "__main__":
with ThreadPoolExecutor(max_workers=5) as executor:
for _ in range(50):
worker = Worker()
executor.submit(worker.run)
@steveg-sys
Copy link

steveg-sys commented Jun 18, 2022

this is can use for windows ?

@lijoabraham
Copy link

lijoabraham commented Aug 23, 2023

@kumaraditya303 Please correct me if am wrong. I guess the line 19 should be like below.

page = context.new_page()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment