在django中异步加载视图或下载文件。
django-celery-async-view的Python项目详细描述
在django中异步加载视图或下载文件。 这是通过在芹菜任务中呈现视图或创建文件并在javascript中轮询来完成的。
快速启动
安装django_芹菜异步视图:
pip install django_celery_async_view
将其添加到已安装的应用程序中
INSTALLED_APPS=(...'django_celery_async_view',...)
功能
这个包包含两个独立的特性:AsyncView和AsyncDownload。
异步视图
示例_app.views.py
classExampleView(AsyncDownloadView):task=example_view_task
示例_app.tasks.py
@shared_taskdefexample_view_task(user_id):returnAsyncViewReturnHTML().run(user_id=user_id,html_string=create_html_example(...))
example_download_page.html
<!-- jQuery before django_celery_async_view --><scripttype="text/javascript"src="{% static "path/to/jquery.js"%}"></script><scripttype="text/javascript"src="{% static "django_celery_async_view/django_celery_async_view.js"%}"></script><script>vartask_id='{{task_id}}';varpoll_interval=10000;AsyncViews.initAsyncView(poll_interval,task_id);</script>
异步下载
示例_app.views.py
classExampleDownloadView(AsyncDownloadView):task=my_download_task
example_async_page.html
<scriptsrc="{% static "django_celery_async_view/django_celery_async_view.js"%}"></script><script>$(document).ready(function(){varpoll_interval=5*1000;// 5s vartask_id='{{task_id}}';AsyncViews.initAsyncView(poll_interval,task_id);});</script>
示例_app.tasks.py
@shared_taskdefexample_download_task(*args,**kwargs):returnMyDownloadCreateFile().run(*args,**kwargs)classExampleDownloadCreateFile(AbstractAsyncDownloadCreateFile):defcreate_file(self,some_arg):# do stuff to create:# file_content, filename, mimetypereturnfile_content,filename,mimetype
example_download_page.html
<!-- jQuery before django_celery_async_view --><scripttype="text/javascript"src="{% static "path/to/jquery.js"%}"></script><scripttype="text/javascript"src="{% static "django_celery_async_view/django_celery_async_view.js"%}"></script><button>DOWNLOAD</button><buttonclass="async-download-button"data-href="/example-download/"data-poll-interval="5000"> Async Download </button>
配置
settings.py
ASYNC_VIEW_TEMP_FILE_DURATION_MS=10*60*1000# 10min
运行示例项目
需要redis。 不需要postgres(测试需要postgres)
# setup # install redis cd example virtualenv venv source venv/bin/activate pip install -r requirements.txt python manage.py migrate # run: # run redis (if not running) celery worker -A example python manage.py runserver
运行测试
测试是针对示例项目运行的? 测试需要postgres和
- env var POSTGRES_PASSWORD set
- or no postgres server authentication
virtualenv venv source venv/bin/activate (myenv) $ pip install tox (myenv) $ tox
开发
python3支持需要修复的内容
- 复制cellerytest项目并将cellerytest设置为absolute
celerytest.__init__.py:1: in <module> from config import CELERY_TEST_CONFIG, CELERY_TEST_CONFIG_MEMORY ImportError: No module named 'config'
- asyncResult(task_id).wait(timeout=5,interval=0.5)和result.get()可能会中断。
example\example\tests\test_async_views.py:50: in phase3 example_view_task.AsyncResult(task_id).wait(timeout=5, interval=0.5) if meta: self._maybe_set_cache(meta) status = meta['status'] if status in PROPAGATE_STATES and propagate: > raise meta['result'] E TypeError: exceptions must derive from BaseException celery\result.py:175: TypeError
历史
0.1.0(2017-11-14)
- pypi上的第一个版本。