2

我大概了解到错误是由于在pyppeteer和requests_html中使用了协程io,这与多线程冲突,但我找不到解决这个问题的方法。我不太会说英语,我使用谷歌翻译。

import asyncio
from pyppeteer import launch
from requests_html import HTMLSession

# Simulation using requests_html

def test1():
    session = HTMLSession()
    _r = session.get('http://bbs.tianya.cn/post-free-6085404-1.shtml' )
    _r.html.render()
    html = _r.html.html

    print(html)


# main
async def main():
    browser = await launch()
    page = await browser.newPage()
    await page.goto('http://example.com')
    await page.screenshot({'path': 'example.png'})
    await browser.close()


##The pyppeteer method is called at work
def aJob(arg):
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    loop.run_until_complete(main())


## Multi-threaded task generation
def multiThread():
    from multiprocessing.dummy import Pool as ThreadPool
    cpus = 1  # 线程池大小

    pool = ThreadPool(cpus)
    _lstParam = range(0, 3)
    pool.map(aJob, _lstParam)
    pool.close()
    pool.join()


if __name__ == "__main__":
    loop = asyncio.new_event_loop()
    multiThread()

我想调用 pyppeteer 或 requests_html 来模拟在多线程中浏览网页,但我总是收到错误“ValueError:信号仅在主线程中有效”或“RuntimeError:线程'Thread-1'中没有当前事件循环。” 试了很多方法,都无法成功运行,请大家帮忙,谢谢!

4

1 回答 1

0

Pyppeteer 使用信号关闭浏览器进程,但信号仅在主线程中有效。如果您真的不需要此功能,只需在 pyppeteer.launch 方法上将 handleSIGINT、handleSIGTERM、handleSIGHUP 设置为 False

于 2021-12-20T03:15:38.590 回答