Last active
June 15, 2024 04:51
-
-
Save kumaraditya303/e6dee949dda298b35d167369955d45c6 to your computer and use it in GitHub Desktop.
MultiThreaded Playwright with ThreadPoolExecutor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
@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
this is can use for windows ?