Skip to content

[INTERNAL] Playwright Sync API does not setup eventloop in thread #710

@kumaraditya303

Description

@kumaraditya303

Reproducible code:

from threading import Thread
from playwright.sync_api import sync_playwright
import asyncio


class TestThread(Thread):
    def run(self):
        with sync_playwright() as playwright:
            browser = playwright.chromium.launch(headless=False)

            browser.new_page()
            browser.close()
        asyncio.get_event_loop()
        # ^^ Throws

test_thread = TestThread()
test_thread.start()
test_thread.join()

Since playwright sync api uses asyncio it is assumed that playwright creates a new eventloop in a thread without loop, but playwright currently does not sets up the event loop after finishing up

Error

Exception in thread Thread-1:
Traceback (most recent call last):
  File "C:\Users\Kumar Aditya\AppData\Local\Programs\Python\Python39\lib\threading.py", line 954, in _bootstrap_inner
    self.run()
  File "D:\playwright-python\main.py", line 13, in run
    asyncio.get_event_loop()
  File "C:\Users\Kumar Aditya\AppData\Local\Programs\Python\Python39\lib\asyncio\events.py", line 642, in get_event_loop
    raise RuntimeError('There is no current event loop in thread %r.'
RuntimeError: There is no current event loop in thread 'Thread-1'.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions