队列管理和代码延迟
项目描述
什么是引擎?
引擎是一组用于处理队列的工具。它提供了一个API来延迟一个可调用的函数并在稍后执行。引擎还提供了进程辅助工具来设置清除不同类型队列的安全环境。
如何使用引擎?
引擎的基本用法是将某事物延迟执行。然而,内部实现是以允许规划的方式编写的。您可以规划代码以使用队列,但队列甚至不需要存在。让我们看一个例子
>>> import engine >>> def hello_world(): ... print "hello world" >>> engine.defer(hello_world) hello world
如上所示,函数 hello_world 立即执行了。这是因为,默认情况下,引擎将使用 DummyQueue,当将其放入队列时将执行给定的可调用函数。
如果您有一个如stompserver、ActiveMQ或任何STOMP兼容队列的队列,您可以使用 StompQueue。让我们看一个例子
>>> import engine >>> def hello_world(): ... print "hello world" >>> engine.defer(hello_world, queue=engine.StompQueue())
上面的代码现在将 hello_world 的调用通过将其发送到队列来延迟。
您可以为引擎使用的默认队列定义。为此,您应使用 engine.configure。以下是将默认队列从 DummyQueue 更改为 StompQueue 的示例
>>> import engine >>> engine.configure(queue=engine.StompQueue())
现在所有对 engine.defer 的调用都将使用 StompQueue 实例。