Python:适用于不同特定工作人员的分布式任务队列

时间:2019-01-22 12:40:58

标签: python distributed task-queue

我正在寻找一个管理任务分配(例如任务队列)的python库/框架。 但是,任务将需要专门的工作人员:工作人员A只能处理a类型的任务,工作人员B和C仅可以处理b类型的任务,等等。 而且,这些工作人员将在不同的计算机上运行并且不能共享相同的代码库(因为就像在生产线中一样,每个任务都必须控制特定的硬件,只有一台计算机可以访问该硬件)。

我看过像python RQ或Celery之类的库,但是如果我理解正确,它们需要相同的代码库才能在不同的worker上运行,并且旨在分发计算。我所寻找的基本上只是一个抽象任务队列的管理和一种工作人员可以通过网络获取任务的机制。然后,任务基本上只是一些有关其进度,错误,结果等的数据和元信息。 如果任务之间也可以相互依赖,那么一个奖励就是可以使一个任务可以依赖于另一个任务的结果。

是否有一个简单的库来管理队列,网络协议等,提供我想要的?

2 个答案:

答案 0 :(得分:1)

我认为您所需要的不是python库,而是适当的队列服务,该队列服务被配置和管理为与python worker完全独立的单元。这样,您无需编写自己的公共库,该库将是所有工作程序的公共依赖项,而是可以将已经存在的程序包重新用于队列订阅。

我真的建议您使用此资源来帮助您实现所要达到的目标,并介绍要查找的术语: https://github.com/donnemartin/system-design-primer#message-queues

我已链接到消息队列部分,其中列出了您可以探索的几个选项:

  • Redis-这实际上是您所建议的-请注意,Redis不是持久性的,您未完成的任务可能会丢失。
  • RabbitMQ-我个人推荐此版本,因为它使用AMQP协议并且拥有庞大的社区。
  • Amazon SQS-如果您使用AWS,则该服务具有在PaaS模型中进行管理的优势。

当然,可以使用队列将任务作为消息分发。

此外,如果您要构建高度异步的系统,则可以探索Event Sourcing模式,因为它会影响整个体系结构以使用消息队列或流服务来传播事件。如果您想走这条路,那么为工作选择合适的服务非常重要。

答案 1 :(得分:0)

This sounds like a great fit for Ray, which is a library for parallel and distributed Python. You can use Ray actors to create "specialized workers". Here's an example with multiple types of workers represented by classes WorkerType1 and WorkerType2.

import ray


@ray.remote
class WorkerType1(object):
    def method1(self):
        # Do some interesting work.
        return 1


@ray.remote
class WorkerType2(object):
    def method2(self):
        # Do some interesting work.
        return 2


if __name__ == "__main__":
    ray.init()

    # Create one worker of each type.
    worker_1 = WorkerType1.remote()
    worker_2 = WorkerType2.remote()

    # Kick off 100 tasks for each worker.
    result_ids = []
    for _ in range(100):
        result_ids.append(worker_1.method1.remote())
        result_ids.append(worker_2.method2.remote())

    # Retrieve the results.
    results = ray.get(result_ids)  # This is [1, 2, 1, 2, ...].

You can of course create a pool of workers of each type. The workers can also have state that the tasks mutate, can have multiple different methods, and so on. You can read more in the Ray documentation.

Note that to run the above example on a cluster instead of a single machine, you need to modify the ray.init() line to pass in a cluster address.

Note that I'm one of the Ray developers.

相关问题