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_taskorth.run_threadsafe - can't run async method from page outside ThreadsafeBrowser Loop, use
await th.create_task - can't run method
run_threadsafein 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()
Release files for PlaywrightSafeThread 0.5.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| playwrightsafethread-0.5.5.tar.gz | 12.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| PlaywrightSafeThread-0.5.5-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 24.3 kB
Release files / playwrightsafethread-0.5.5.tar.gz
| Download URL | playwrightsafethread-0.5.5.tar.gz |
|---|---|
| Size | 12.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8bb25cdec2a4c6c6b8a1c784f37028ee1d6d3c4b505b76c041eeada9cbc2c880
|
|
BLAKE2b-256 checksum How to use checksums |
41f2af2b7493f6584b96c8b3f24487cb0c581fc44a7458a57eba93daffdfb63d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.9.19
|
Release files / PlaywrightSafeThread-0.5.5-py3-none-any.whl
| Download URL | PlaywrightSafeThread-0.5.5-py3-none-any.whl |
|---|---|
| Size | 12.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
6225a7cb55d1661c334fa034c4f270f018e455f67badf5af12031469f31091a5
|
|
BLAKE2b-256 checksum How to use checksums |
35aed86abb887a6b908942786811d4f2daefe0f5d11a16484d286c67f990375f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.9.19
|