在django中异步加载视图或下载文件。

django-celery-async-view的Python项目详细描述


https://travis-ci.org/EeroPaukkonen/django_celery_async_view.svg?branch=masterhttps://codecov.io/gh/EeroPaukkonen/django_celery_async_view/branch/master/graph/badge.svg

在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和

  1. env var POSTGRES_PASSWORD set
  2. or no postgres server authentication
virtualenv venv
source venv/bin/activate
(myenv) $ pip install tox
(myenv) $ tox

开发

python3支持需要修复的内容

  1. 复制cellerytest项目并将cellerytest设置为absolute
celerytest.__init__.py:1: in <module>
from config import CELERY_TEST_CONFIG, CELERY_TEST_CONFIG_MEMORY
ImportError: No module named 'config'
  1. 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上的第一个版本。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
ArrayList Java中的搜索字符串   另一个web应用程序的java访问会话   另一个应用程序中的活动和服务之间的java通信   java根据Json字符串类型将Json字符串转换为对象   eclipse如何解决java中的错误异常。lang.NoSuchMethodError:'java。字符串javax。摆动JOptionPane。showInputDialog(java.lang.String)'   线程“main”java中的安卓异常。lang.NoClassDefFoundError:org/codehaus/jackson/JsonParseException   java如何在安卓 emulator上显示Mat图像?使用NDK   Java在本地读取测试源文件,但在服务器上读取失败   java dowhile循环用于计算输入数字中的数字。故障排除代码   JAva初学者在编写获取成本的方法时遇到困难   java是shell游戏。我如何让物体移动,特别是在特定的曲线上,但顺序是随机的?   java如何区分两个同名的JButton   java为什么我在Spring Boot中需要一个接口?   java将文件路径插入数据库将删除\   使用InterfaceType初始化java对象   java如何部署一个分为Angular、Spring Boot和MySQL的项目?   java如何使用Symja解决不等式?