Rayrun
← Back to Discord Forum

I have a custom scheduler, playwright keeps closing before the other function is executed. (Python)

class Scheduler:
    def __init__(self):
        self.execution_array: List[Callable[[], None]] = []

    def add_execution(self, fn: List[Callable[[], None]] = []):
        self.execution_array.append(fn)

    def run(self):
        while True:
            if self.execution_array[0] is not None and len(self.execution_array):
                self.execution_array[0]()
                self.execution_array.remove(self.execution_array[0])```

This thread is trying to answer question "Why does Playwright close before another function in a custom scheduler is executed, and how can a browser object be launched and stored for later use without this issue?"

5 replies

raise Error("Event loop is closed! Is Playwright already stopped?") playwright._impl._api_types.Error: Event loop is closed! Is Playwright already stopped?

def launch_browser():
        # Start Playwright and connect to Chromium browser
        with playwright.sync_api.sync_playwright() as p:
            global browser, context, page
            browser = p.chromium.launch(headless=False)
            context = browser.new_context(user_agent=random.choice(user_agents))
            page = context.new_page()

    scheduler.add_execution(launch_browser)```

I'm trying to launch it then store the browser object to use later, why is that not possible?

I have a custom scheduler, playwright keeps closing before the other function is executed. (Python)

I'm using sync btw

Related Discord Threads

TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email luc@ray.run.