[Python] Running pyppeteer in Flask gives 'ValueError: signal only works in main thread'

Discussão em 'Python' iniciado por Stack, Junho 6, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to integrate pyppeteer in a Flask app. I have a Python script that runs pyppeteer and takes a screenshot of a page. This is working fine if I run the script individually.

    The problem is that the same script does not work when I run it in a Flash app.

    I get the following error:

    loop.run_until_complete(capture(url, 123123))
    File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/
    Versions/3.7/lib/python3.7/asyncio/base_events.py",
    line 568, in run_until_complete
    return future.result()
    File "/App-path/flaskr/image_capture/__init__.py", line 6, in capture
    browser = await launch()
    File "/usr/local/lib/python3.7/site-packages/pyppeteer/launcher.py",
    line 311, in launch
    return await Launcher(options, **kwargs).launch()
    File "/usr/local/lib/python3.7/site-packages/pyppeteer/launcher.py",
    line 180, in launch
    signal.signal(signal.SIGINT, _close_process)
    File"/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/
    Versions/3.7/lib/python3.7/signal.py", line 47, in signal
    handler = _signal.signal(_enum_to_int(signalnum),
    _enum_to_int(handler))
    ValueError: signal only works in main thread.


    The following code is used to capture a screenshot:

    async def capture(code_url, codeId):
    browser = await launch()
    # print('Hello')
    page = await browser.newPage()
    await page.setContent('<div id="chart-container">ABCD</div>')
    # print(await page.content())
    await page.addScriptTag({'url':'''{code_url}'''})
    await page.waitFor('.animateon')
    await page.setViewport({
    'width':await
    page.evaluate('''document.documentElement.clientWidth''') ,
    'height': await
    page.evaluate('''document.documentElement.clientHeight'''),
    'deviceScaleFactor': 10,
    })
    await page.screenshot({'path': '''./temp/screenshot/chart-
    {codeId}.jpg''', 'type': 'jpeg'})
    await browser.close()


    The following code is where this method is called:

    @app.route('/api/v1/screenshot', methods=["POST"])
    def screenShot():
    url = request.form['url']
    loop.run_until_complete(capture(url, 123123))
    return jsonify("Image captured Successfully!")


    I am using asyncio loop to handle the async capture function. I have turned off the debugging mode.

    Please suggest where I am going wrong.

    Python version: 3.7

    Continue reading...

Compartilhe esta Página