【问题标题】:Transfer Large Files Asynchronously In Flask在 Flask 中异步传输大文件
【发布时间】:2017-05-30 02:20:44
【问题描述】:
在 Flask 中异步传输大文件的最佳方法是什么?我读过这个article。但我想知道是否有办法不使用芹菜来做到这一点?
【问题讨论】:
标签:
asynchronous
flask
python-3.5
python-asyncio
aiohttp
【解决方案1】:
Flask 是一个同步框架,您可以尝试 flask+gevent 和流式响应,如下所述:http://flask.pocoo.org/docs/0.12/patterns/streaming/。
无论如何,如果您想正确上传非常大的文件,我建议您使用不同的方法。与其尝试使用同步框架进行异步网络,不如尝试使用 Nginx upload_module 委托传输,如下所述:http://blog.thisisfeifan.com/2013/03/nginx-upload-module-vs-flask.html
Nginx 速度更快,并且不会将文件加载到内存中,即使在异步模式下,Flask 或 Django 等常规框架也可以做到这一点。请记住使用指令upload_pass 将flask 配置为在上传POST 后接收。唯一需要注意的是,您必须学习如何从源代码编译完整的 Nginx,这里是工作 Dockerfile 的示例:
FROM buildpack-deps:jessie
##### NGINX #####
# Base Stuff
RUN apt-get update && apt-get install -y -qq \
libssl-dev
# Nginx with upload_module and upload_progress_module
# "Stable version".
ENV ZLIB_VERSION 1.2.11
ENV PCRE_VERSION 8.39
ENV NGX_UPLOAD_MODULE_VERSION 2.2
ENV NGX_UPLOAD_PROGRESS_VERSION 0.9.1
ENV NGX_HEADERS_MORE_VERSION 0.32
ENV NGX_SPPEDPAGE_VERSION 1.11.33.4
ENV NGINX_VERSION 1.11.8
RUN cd /tmp \
&& wget http://nginx.org/download/nginx-${NGINX_VERSION}.tar.gz \
&& tar xvf nginx-${NGINX_VERSION}.tar.gz \
&& wget https://github.com/openresty/headers-more-nginx-module/archive/v${NGX_HEADERS_MORE_VERSION}.tar.gz \
&& tar -xzvf v${NGX_HEADERS_MORE_VERSION}.tar.gz \
&& wget https://github.com/pagespeed/ngx_pagespeed/archive/latest-stable.tar.gz \
&& tar -xzvf latest-stable.tar.gz \
&& wget https://dl.google.com/dl/page-speed/psol/${NGX_SPPEDPAGE_VERSION}.tar.gz \
&& tar -xzvf ${NGX_SPPEDPAGE_VERSION}.tar.gz \
&& mv psol ngx_pagespeed-latest-stable/ \
&& git clone -b ${NGX_UPLOAD_MODULE_VERSION} https://github.com/Austinb/nginx-upload-module \
&& wget http://zlib.net/zlib-${ZLIB_VERSION}.tar.gz \
&& tar xvf zlib-${ZLIB_VERSION}.tar.gz \
&& wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-${PCRE_VERSION}.tar.bz2 \
&& tar -xjf pcre-${PCRE_VERSION}.tar.bz2 \
&& wget https://github.com/masterzen/nginx-upload-progress-module/archive/v${NGX_UPLOAD_PROGRESS_VERSION}.tar.gz \
&& tar xvf v${NGX_UPLOAD_PROGRESS_VERSION}.tar.gz \
&& cd nginx-${NGINX_VERSION} \
&& ./configure \
--with-pcre=../pcre-${PCRE_VERSION}/ \
--with-zlib=../zlib-${ZLIB_VERSION}/ \
--add-module=../nginx-upload-module \
--add-module=../nginx-upload-progress-module-${NGX_UPLOAD_PROGRESS_VERSION} \
--add-module=../ngx_pagespeed-latest-stable \
--add-module=../headers-more-nginx-module-${NGX_HEADERS_MORE_VERSION} \
--with-select_module \
--with-poll_module \
--with-file-aio \
--with-http_ssl_module \
--with-ipv6 \
--with-pcre-jit \
--with-http_gzip_static_module \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--user=nginx --group=nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --with-cpu-opt=CPU -- with-ld-opt="-Wl,-E" \
&& make \
&& make install
EXPOSE 80 443
CMD ["nginx", "-g", "daemon off;"]
注意:请在此图片中缺少 nginx.conf 和 default.conf 的设置。