nest_asyncio
'sync'
def Sync():
import asyncio, functools, importlib.util as U
U.find_spec('nest_asyncio') or os.system('pip install -q nest_asyncio')
__import__('nest_asyncio').apply(); return lambda func: functools.wraps(
func)(lambda *args, **kwargs: asyncio.run(func(*args, **kwargs)))
sync = Sync()
'playwright'
@sync
async def Page():
if not hasattr(Page, 'context'):
from playwright.async_api import async_playwright
playwright = await async_playwright().start()
browser = await playwright.firefox.launch(timeout=0)
Page.context = await browser.new_context()
page = await Page.context.new_page()
for attr in dir(page):
if callable(method := getattr(page, attr)) and not attr.startswith('_'):
setattr(page, attr, sync(method))
return page
'main'
if __name__ == '__main__':
page = Page()
page.goto('http://example.com')
print(page.title()) # Example Domain
threading
import asyncio, threading
try:
asyncio.get_running_loop()
asyncio.run = lambda main: threading.Thread(
target=lambda: asyncio.runners.run(main)).start()
except: pass