ponty / pyscreenshot

Python screenshot library, replacement for the Pillow ImageGrab module on Linux.
BSD 2-Clause "Simplified" License
502 stars 88 forks source link

pyscreenshot.grab() causes flask to restart #57

Closed KumaTea closed 4 years ago

KumaTea commented 5 years ago

I deployed a Telegram bot service, using Python 3.7, on a server running Windows Server 2008 R2. Recently I found that pyscreenshot.grab() will cause flask to restart.

In the main.py:

from flask import Flask, request as flaskreq
# other imports are omitted

starting()  # When finished this function prints "Successfully startup."

app = Flask(__name__)

@app.route('/', methods=['POST'])
def main():
    data = flaskreq.json
    # print(data)
    resp = msgtype(data)
    logcsv(data, resp)
    return '', 200

if __name__ == '__main__':
    app.run(host='localhost', port=10568, debug=False)

In a module used to debug:

import os
import pyscreenshot as scrshot
# other imports are omitted
...

def mddebug(data):
    adminid = getadminid()
    debugmsg = json.dumps(data)
    sendmsg(adminid, debugmsg)
    sendfile(adminid, 'log/log.csv', False, 'upload')
    if os.name == 'nt':

scrst = scrshot.grab() scrst.save('log/screenshot.png')

        sendphoto(adminid, 'log/screenshot.png', False, 'upload')
    return 'DEBUG FINISHED'

...

When the function mddebug is triggered, and just between the log.csv and screenshot.png is sent in order, the terminal prints something like:

127.0.0.1 - - [18/May/2019 02:28:28] "POST / HTTP/1.1" 200 -
Successfully startup.
127.0.0.1 - - [18/May/2019 02:28:49] "POST / HTTP/1.1" 200 -

, which indicates a restart of the main.py file.

I found that a normal python script won't reproduce this issue, while scripts using flask will. I can't make sure if the restart is caused by pyscreenshot or flask.

I will appreciate it if you could tell the cause or ways to avoid this problem. Thanks.

ponty commented 5 years ago

Please check the README.rst: "Replacement for the ImageGrab Module, which works on Windows only, so Windows users don't need this library. "

You need this lib: https://pillow.readthedocs.io/en/latest/reference/ImageGrab.html