带有线程的Flask服务器给出了错误:[Errno 32]管道损坏"

时间:2017-10-16 18:49:20

标签: python flask

我在Flask开发服务器中启用了threaded,但它似乎没有修复" Broken pipe" Flask broken pipe with requests中描述的错误。

from flask import Flask, request
import requests

app = Flask(__name__)

@app.route('/compare', methods=['POST'])
def compare():
    data = request.get_json()
    img = data['img']
    imgdata = requests.get(img).content  # Error is from here
    filename = 'hello.jpg'

    with open(filename, 'wb') as f:
        f.write(imgdata)

    return 'Yes'

if __name__ == '__main__':
    app.run(threaded=True, host='0.0.0.0', port=80)
Traceback (most recent call last):
  File "/usr/lib/python2.7/SocketServer.py", line 593, in process_request_thread
    self.finish_request(request, client_address)
  File "/usr/lib/python2.7/SocketServer.py", line 334, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "/usr/lib/python2.7/SocketServer.py", line 651, in __init__
    self.finish()
  File "/usr/lib/python2.7/SocketServer.py", line 710, in finish
    self.wfile.close()
  File "/usr/lib/python2.7/socket.py", line 279, in close
    self.flush()
  File "/usr/lib/python2.7/socket.py", line 303, in flush
    self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 32] Broken pipe

1 个答案:

答案 0 :(得分:0)

可能是因为连接过早关闭或文件太大。试试这个:

api = tweepy.API(auth)


#Twitter's words on API limits https://support.twitter.com/articles/15364

#### Define twitter rate determining loop
def twitter_rates():
    stats = api.rate_limit_status()  #stats['resources'].keys()
    for akey in stats['resources'].keys():
        if type(stats['resources'][akey]) == dict:
            for anotherkey in stats['resources'][akey].keys():
                if type(stats['resources'][akey][anotherkey]) == dict:
                    #print(akey, anotherkey, stats['resources'][akey][anotherkey])
                    limit = (stats['resources'][akey][anotherkey]['limit'])
                    remaining = (stats['resources'][akey][anotherkey]['remaining'])
                    used = limit - remaining
                    if used != 0:
                        print("Twitter API used", used, "remaining queries", remaining,"for query type", anotherkey)
                    else:
                        pass
                else:
                    pass  #print("Passing")  #stats['resources'][akey]
        else:
            print(akey, stats['resources'][akey])
            print(stats['resources'][akey].keys())
            limit = (stats['resources'][akey]['limit'])
            remaining = (stats['resources'][akey]['remaining'])
            used = limit - remaining
            if used != 0:
                print("Twitter API:", used, "requests used,", remaining, "remaining, for API queries to", akey)
                pass


twitter_rates()