django的平凡postgres队列

django-tpq的Python项目详细描述


Travis CI StatusCode Coveragehttps://badge.fury.io/py/django-tpq.svg

django的平凡postgres队列

这是一个django应用程序,它集成了 tpq。此应用程序提供 消息队列功能以及高级期货实现。

消息队列

要实现消息队列,必须首先创建从 抽象BaseQueue模型。完成后,为该模型创建一个迁移,然后 编辑迁移以添加一些额外的数据库对象(tpq为 你,但你必须从迁移中调用它)。然后迁移并使用队列。

fromdjango_tpq.main.modelsimportBaseQueueclassMyQueue(BaseQueue):pass
$ python manage.py makemigrations

现在编辑迁移并添加runpython步骤 initial migration。 您还需要在forward函数中自定义模型名。

$ python manage.py migrate
frommyapp.modelsimportMyQueueMyQueue.objects.enqueue({'field':'value'})message=MyQueue.objects.dequeue()

未来

以上述为基础,提出了一种简单的期货实现方案。 首先,您必须注册任何希望作为将来异步调用的函数。 那就称之为未来吧。您可以选择等待或轮询结果。

importtimefromdjango_tpq.futures.decoratorsimportfuture@future()deflong_running_function(*args):time.sleep(100)# You can execute the future without waiting for a result. This returns# immediately and your future runs within another process (fire and forget).long_running_function.async('argument_1')# Or you can poll for the results (or check after you do some other work).f=long_running_function.async('argument_1')whileTrue:try:r=f.result()exceptException:# Exceptions are re-raised.LOGGER.exception('Future failed',exc_info)breakelse:breaktime.sleep(10)print(r)# Or optionally, you can block waiting for the result.f=long_running_function.call('argument_1')try:r=f.result(wait=0)exceptException:# Exceptions are re-raised.LOGGER.exception('Future failed',exc_info)print(r)

函数调用通过消息队列进行调度。争论是有问题的,所以你 可以发送任何可选取的python对象。结果通过配置的 隐藏物。默认情况下使用default缓存,但可以使用 FUTURES_RESULT_CACHE设置为django提供备用名称 要用于结果的缓存。结果的TTL为60分钟 默认值,但可以使用FUTURES_RESULT_TTL设置对此进行调整。

*注意,如果使用非常短的ttl并在它已经 过期,你将永远看不到结果。此外,如果你使用wait,你将等待 永远。

期货由使用django管理命令启动的守护进程执行。

$ python manage.py futures_executor --help
usage: manage.py futures_executor [-h] [--version] [-v {0,1,2,3}]
                                  [--settings SETTINGS]
                                  [--pythonpath PYTHONPATH] [--traceback]
                                  [--no-color] [--queue_name QUEUE_NAME]
                                  [--once] [--wait WAIT]

Daemon to execute futures.

optional arguments:
  -h, --help            show this help message and exit
  --version             show program's version number and exit
  -v {0,1,2,3}, --verbosity {0,1,2,3}
                        Verbosity level; 0=minimal output, 1=normal output,
                        2=verbose output, 3=very verbose output
  --settings SETTINGS   The Python path to a settings module, e.g.
                        "myproject.settings.main". If this isn't provided, the
                        DJANGO_SETTINGS_MODULE environment variable will be
                        used.
  --pythonpath PYTHONPATH
                        A directory to add to the Python path, e.g.
                        "/home/djangoprojects/myproject".
  --traceback           Raise on CommandError exceptions
  --no-color            Don't colorize the command output.
  --queue_name QUEUE_NAME
                        The queue to monitor. default: futures.FutureQueue
  --once                Run one, then exit.
  --wait WAIT           Wait time. Useful with --once.

一些未来的统计数据也存储在postgres数据库中以供报告 目的。

fromdjango_tpq.futures.modelsimportFutureStatFutureStat.objects.all()

FutureStat模型具有以下字段。

  • name-未来的python module.function。
  • running-当前正在执行此类型的未来的数目。
  • total-此类型的已执行未来的总数。
  • failed-导致异常的期货数量。
  • last_seen-未来最新执行的时间戳。
  • first_seen-未来最近执行的时间戳。

作为一个模型,您可以使用django orm以任何方式报告这些字段 请参见配合。

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

推荐PyPI第三方库


热门话题
Spring、JPA和hibernate的java问题   如何将JMC(Java任务控制)连接到ubuntu中的远程JVM?   java如何将eventListener的结果存储为对象?   java在安卓中,点击一个按钮,我如何停止发送循环中发送的消息   java打开活动中的电子邮件   使用velocity模板打印JasperReports   java无法在自定义信息窗口上拨号   java如何在jsonb postgresql中查询并转换为谓词JPA   java更好地理解J2EE环境中的异常和日志记录   java打印多个文件   java无法使用API v2 Foreman 1.7.1创建主机   HTML单一提交类型按钮在java中不起作用   java调用静态方法的实例   ViewPage中替换片段的java问题   C++在java中创建数组(2D)而不初始化内部数组   java如何在NetBeans中同时更改变量名称的多个实例?   如何完成这个关于集合的java程序   java如何选择使用selenium从下拉菜单动态生成的元素?