Flask RESTful API request, Broken pipe [Errno 32]

2019-09-02 00:29发布

问题:

I'm new to web development and I'm trying to create a RESTful web service using the Flask micro-framework.

Here is my code:

app = Flask(__name__)       

client = MongoClient()
db = client.markets

def toJson(data):
    return json.dumps(data, default=json_util.default)

@app.route('/', methods=['GET'])

def get_tasks():
    cursor = db.europe.find()
    list = []

    for i in cursor:
        list.append(i)

    return toJson(list)

When I send the request from my browser, it is constantly waiting for the server and nothing is returned.

Eventually I will see the flask server running in the terminal will give me: [Errno 32] Broken pipe.

My collection has 1.5 million entries, each with about 20 attributes. Could it be because the request is too large?

Thanks in advance.

回答1:

The Broken pipe indicates that the other end of a socket or pipe that your flask process wants to talk to has died. Considering that you are interacting with the database it's very likely that the database has terminated the connection or the connection has died for other reasons.

Probably you should be analyzing the query that you run on your db, because the code itself doesn't seem to have an obvious problem.

Try running the query on your MongoDB manually and see what happens. Does the query return successfully?

You're mentioning that it takes a lot of time until you get that error. Could it be that some indexes are missing or not properly used in your schema, which makes the query execute very slow, and after waiting for a long time it reaches a timeout (f.e. maxTimeMS)?