简单的离线任务队列。
项目描述
简单的离线任务队列。适用于Python。
“回头见,鳄鱼。”
最新文档请访问 http://alligator.readthedocs.org/en/latest/。
需求
Python 3.6+
(可选) 用于Redis后端的redis
(可选) 用于SQS后端的boto3>=1.12.0
基本用法
此示例使用Django,但Alligator与Django没有特定关联。
我再说一遍,您可以将其与任何可以从后台处理中受益的Python代码一起使用。
from alligator import Gator
from django.contrib.auth.models import User
from django.shortcuts import send_email
# Make a Gator instance.
# Under most circumstances, you would configure this in one place &
# import that instance instead.
gator = Gator('redis://localhost:6379/0')
# The task itself.
# Nothing special, just a plain *undecorated* function.
def follow_email(followee_username, follower_username):
followee = User.objects.get(username=followee_username)
follower = User.objects.get(username=follower_username)
subject = 'You got followed!'
message = 'Hey {}, you just got followed by {}! Whoohoo!'.format(
followee.username,
follower.username
)
send_email(subject, message, 'server@example.com', [followee.email])
# An simple, previously expensive view.
@login_required
def follow(request, username):
# You'd import the task function above.
if request.method == 'POST':
# Schedule the task.
# Use args & kwargs as normal.
gator.task(follow_email, request.user.username, username)
return redirect('...')
运行任务
而不是尝试自动发现、广播等,您控制您的工人的配置和它们消费的内容。
如果您的需求简单,运行包含的latergator.py 工人
$ python latergator.py redis://localhost:6379/0
如果您有更复杂的需求,您可以创建一个新的可执行文件(bin脚本、管理命令等)并将以下代码放入其中。
from alligator import Gator, Worker
# Bonus points if you import that one pre-configured ``Gator`` instead.
gator = Gator('redis://localhost:6379/0')
# Consume & handle all tasks.
worker = Worker(gator)
worker.run_forever()
许可证
新BSD
运行测试
鳄鱼有95%+的测试覆盖率,并旨在始终通过/稳定。
如果您想运行测试,克隆仓库,然后运行
$ virtualenv -p python3 env $ . env/bin/activate $ pip install -r requirements-tests.txt $ python setup.py develop $ pytest -s -v --cov=alligator --cov-report=html tests
可以通过以下方式运行完整的测试套件
$ export ALLIGATOR_TESTS_INCLUDE_SQS=true $ ./tests/run_all.sh
这需要所有后端/队列都在运行状态,如果设置了 ALLIGATOR_TESTS_INCLUDE_SQS=true,还需要有效的 AWS 凭据。
为什么?!1!
因为我有 NIH 综合症。
或者因为我渴望一些简单的东西(约 375 行代码)。
或者因为我想要有测试(90%+覆盖率)和文档的东西。
或者因为我想要可插拔的后端。
或者因为测试其他队列系统很痛苦。
或者因为我是个白痴。
路线图
1.0.0 版本之后
- 扩展支持的本地后端
支持 Kafka 吗?
ActiveMQ 支持?
RabbitMQ 支持?
???
项目详情
下载文件
下载适合您平台的应用程序。如果您不确定选择哪个,请了解更多关于 安装包 的信息。
源代码分发
alligator-1.0.0.tar.gz (15.3 kB 查看哈希值)
构建版本
alligator-1.0.0-py3-none-any.whl (17.8 kB 查看哈希值)