PlaywrightSafeThread
Project description
PlaywrightSafeThread
use playwright as async and sync safe thread
used in WPP_Whatsapp
use sync in only one at time, else use async
in events like page.on
use async and wait
from minet
Installation
pip install PlaywrightSafeThread
pip install git+https://www.github.com/3mora2/PlaywrightSafeThread@main
Example
- to run async method in like page, user
await th.create_task
orth.run_threadsafe
- can't run async method from page outside ThreadsafeBrowser Loop, use
await th.create_task
- can't run method
run_threadsafe
in ThreadsafeBrowser Loop
Sync
from PlaywrightSafeThread import ThreadsafeBrowser
th = ThreadsafeBrowser(
install=False,
no_context=False,
browser="chromium", channel="chrome", headless=False,
no_viewport=True, bypass_csp=True
)
th.goto_sync("https://web.whatsapp.com/", wait_until="networkidle")
# or
th.run_threadsafe(th.page.goto("https://web.whatsapp.com/", wait_until="networkidle"))
th.sync_close()
Async
Use asyncio.run
import asyncio
from PlaywrightSafeThread import ThreadsafeBrowser
async def main():
th = ThreadsafeBrowser(
install=False,
no_context=False,
browser="chromium", channel="chrome", headless=False,
no_viewport=True, bypass_csp=True, # loop=loop
)
# Work Only When start from th loop, not in (asyncio.run(main()))
# await th.page.goto("https://web.whatsapp.com/", wait_until="networkidle")
await th.goto("https://web.whatsapp.com/")
# or
await th.create_task(
th.page.goto("https://web.whatsapp.com/")
)
# Not Work when start from th loop, work in only (asyncio.run(main()))
th.goto_sync("https://web.whatsapp.com/", wait_until="networkidle")
await th.close()
asyncio.run(main())
Use ThreadsafeBrowser.loop
import asyncio
from PlaywrightSafeThread import ThreadsafeBrowser
th = ThreadsafeBrowser(
install=False,
no_context=False,
browser="chromium", channel="chrome", headless=False,
no_viewport=True, bypass_csp=True, # loop=loop
)
loop = th.loop
async def main():
await th.page.goto("https://web.whatsapp.com/", wait_until="networkidle")
await th.create_task(
th.page.goto("https://web.whatsapp.com/")
)
await th.goto("https://web.whatsapp.com/")
# Not Work when start from th loop, work in only (asyncio.run(main()))
# th.goto_sync("https://web.whatsapp.com/", wait_until="networkidle")
await th.close()
task = asyncio.run_coroutine_threadsafe(main(), loop=loop)
task.result()
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for PlaywrightSafeThread-0.5.4.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | f8669e2defee8910cde19e41613a596b6a2b42bd92e1435bdb7dbf7872708248 |
|
MD5 | e0a2b8125fdd7ef1c8cd6502f442b404 |
|
BLAKE2b-256 | 8d4c4e05523872006ea4f83a502acd9d83a5f57fdfd8400a180ff8d55af9d401 |
Close
Hashes for PlaywrightSafeThread-0.5.4.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1bdba5bc7eace57f90a22179ac09db03d319e8e82da67e177f10b2d192ca7a30 |
|
MD5 | a09d9b3d84403bcc9a0e34fab854e4b0 |
|
BLAKE2b-256 | ad57ebab15b2ce3fa8899e11747559e2e7cb45d68609cf85e83f30db518d84b7 |