Running pypupeteer in FLASK gives ValueError: signal only works in main thread

社会主义新天地 提交于 2020-05-27 04:58:05

问题


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

The PROBLEM is the same script does not work when i run it in a FLASK 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 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.

Also as per some suggestion from Stackoverflow questions I have turned off the debugging mode.

Please suggest where I am going wrong.

Python Version : 3.7


回答1:


You need to call launch with disabled signals handling,

browser = await launch(
    handleSIGINT=False,
    handleSIGTERM=False,
    handleSIGHUP=False
)


来源:https://stackoverflow.com/questions/53679905/running-pypupeteer-in-flask-gives-valueerror-signal-only-works-in-main-thread

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!