The python program needs to run pyppeteer in a thread, and the result is reported the following message [ValueError: signal only works in main thread]

Reference: https://blog.csdn.net/weixin_41822224/article/details/103719863

Must be increased in multiple threads to run

"handleSIGINT": False,
            "handleSIGTERM": False,
            "handleSIGHUP": False

import asyncio
from pyppeteer import launch
import time
from app.libs.thread.own_thread import OwnThread

is_mobile_browser = False

_defaultViewport = {
    "width": 375,
    "height": 668,
    "isMobile": True,
    "hasTouch": True,
} if is_mobile_browser else {
    "width": 998,
    "height": 800,
    "isMobile": False,
    "hasTouch": False,
}


async def temp_func():
    browser = await launch(
        options={
            'headless': False,
            'args': ['--no-sandbox'],
            "defaultViewport": _defaultViewport,
            "handleSIGINT": False,
            "handleSIGTERM": False,
            "handleSIGHUP": False
        })
    page = await browser.newPage()
    await page.setJavaScriptEnabled(enabled=True)
    await page.goto('www.baidu.com')

    ele = await page.J(".main")

    print(await (await ele.getProperty("src")).jsonValue())
    time.sleep(30)
    await browser.close()


OwnThread(lambda: asyncio.run(temp_func())).start()

print("aaaa")

 

Guess you like

Origin blog.csdn.net/weixin_43343144/article/details/112250241