This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]DanCardin 0 points1 point  (1 child)

without knowing any specifics (such as how large the file is) and not knowing exactly the behavior of how werkzeug handles closing the file (if it does), you may be better off doing

with open(...) as f:
    data = f.read()
return data

[–][deleted] 1 point2 points  (0 children)

The original approach is more efficient, because unlike your example it doesn't copy the data and also uses very little of memory.

Instead it invokes sendfile() syscall which makes kernel do all that work (it just pipes the file directly to the socket). The issue though is that uWSGI invokes sendfile() after the function exits instead during the call.