Twisted or Celery? Which is right for my applicati

2019-03-09 01:37发布

问题:

I'm writing a Python application that needs both concurrency and asynchronicity. I've had a few recommendations each for Twisted and Celery, but I'm having trouble determining which is the better choice for this application (I have no experience with either).

The application (which is not a web app) primarily centers around making SOAP calls out to various third party APIs. To process a given piece of data, I'll need to call several APIs sequentially. And I'd like to be able to have a pool of "workers" for each of these APIs so I can make more than 1 call at a time to each API. Nothing about this should be very cpu-intensive.

More specifically, an external process will add a new "Message" to this application's database. I will need a job that watches for new messages, and then pushes them through the Process. The process will contain 4-5 steps that need to happen in order, but can happen completely asynchronously. Each step will take the message and act upon it in some way, typically adding details to the message. Each subsequent step will require the output from the step that precedes it. For most of these Steps, the work involved centers around calling out to a third-party API typically with a SOAP client, parsing the response, and updating the message. A few cases will involve the creation of a binary file (harder to pickle, if that's a factor). Ultimately, once the last step has completed, I'll need to update a flag in the database to indicate the entire process is done for this message.

Also, since each step will involve waiting for a network response, I'd like to increase overall throughput by making multiple simultaneous requests at each step.

Is either Celery or Twisted a more generally appropriate framework here? If they'll both solve the problem adequately, are there pros/cons to using one vs the other? Is there something else I should consider instead?

回答1:

Is either Celery or Twisted a more generally appropriate framework here?

Depends on what you mean by "generally appropriate".

If they'll both solve the problem adequately, are there pros/cons to using one vs the other?

Not an exhaustive list.

Celery Pros:

  • Ready-made distributed task queue, with rate-limiting, re-tries, remote workers
  • Rapid development
  • Comparatively shallow learning curve

Celery Cons:

  • Heavyweight: multiple processes, external dependencies
  • Have to run a message passing service
  • Application "processes" will need to fit Celery's design

Twisted Pros:

  • Lightweight: single process and not dependent on a message passing service
  • Rapid development (for those familiar with it)
  • Flexible
  • Probably faster, no "internal" message passing required.

Twisted Cons:

  • Steep learning curve
  • Not necessarily as easy to add processing capacity later.

I'm familiar with both, and from what you've said, if it were me I'd pick Twisted.

I'd say you'll get it done quicker using Celery, but you'd learn more while doing it by using Twisted. If you have the time and inclination to follow the steep learning curve, I'd recommend you do this in Twisted.



回答2:

Celery allows you to use asynchronous behavior of various async library like gevent and eventlet. So you can have best of both world.

Example using eventlet https://github.com/celery/celery/tree/master/examples/eventlet

Example using gevent https://github.com/celery/celery/tree/master/examples/gevent