diff --git a/playwright/README.md b/playwright/README.md index 6bdb4bd..66c3409 100644 --- a/playwright/README.md +++ b/playwright/README.md @@ -5,24 +5,21 @@ Linux (WSL Debian) pip install playwright playwright install --with-deps """ +def sync(func): + import asyncio, threading, queue, functools; asyncio.set_event_loop_policy(None) + def server(): sync.q.get(); f, a, kw = sync.obj; sync.obj = f(*a, **kw); sync.q.task_done(); server() + if not hasattr(sync, 'q'): sync.q = queue.Queue(); threading.Thread(target=server).start() + @functools.wraps(func) + def wrapper(*a, **kw): sync.obj = func, a, kw; sync.q.put(1); sync.q.join(); return sync.obj + return wrapper +@sync def Page(): - def sync(func): - import asyncio, threading, queue, functools; asyncio.set_event_loop_policy(None) - def server(): - while sync.q.get(): f, a, kw = sync.obj; sync.obj = f(*a, **kw); sync.q.task_done() - if not hasattr(sync, 'q'): sync.q = queue.Queue(); threading.Thread(target=server).start() - @functools.wraps(func) - def wrapper(*a, **kw): sync.obj = func, a, kw; sync.q.put(True); sync.q.join(); return sync.obj - return wrapper - @sync - def Page(): - from playwright.sync_api import sync_playwright - (context := sync_playwright().start().firefox.launch().new_context()).set_default_timeout(0) - for attr in dir(page := context.new_page()): - if attr[0] != '_' and callable(method := getattr(page, attr)): setattr(page, attr, sync(method)) - page._repr_png_ = page.screenshot - return page - return Page() + from playwright.sync_api import sync_playwright + (context := sync_playwright().start().firefox.launch().new_context()).set_default_timeout(0) + for attr in dir(page := context.new_page()): + if attr[0] != '_' and callable(method := getattr(page, attr)): setattr(page, attr, sync(method)) + page._repr_png_ = page.screenshot + return page (page := Page()).goto('https://example.org') print(page.title()) # Example Domain ``` \ No newline at end of file