Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 834
UJJAL DUTTA Avatar asked Dec 08 '18 05:12

UJJAL DUTTA


1 Answers

You need to call launch with disabled signals handling,

browser = await launch(
    handleSIGINT=False,
    handleSIGTERM=False,
    handleSIGHUP=False
)
like image 60
ssubbotin Avatar answered Nov 17 '22 15:11

ssubbotin