Best way to create REST API for long lasting tasks?

后端 未结 3 1471
春和景丽
春和景丽 2021-02-13 15:39

Suppose I have 2 servers.

The first is a service that provides some computations, which can last long time (minutes to hours).

The second server will use this s

相关标签:
3条回答
  • 2021-02-13 15:53

    In addition to what I've already answered in this similar question, I'd suggest using the Atom Publishing Protocol for the notification (you could publish to your second server).

    0 讨论(0)
  • 2021-02-13 15:57

    If you use Python, you can take advantage of RabbitMQ and Celery to do the job. Celery lets you create an item in a queue and then pause execution of whatever you're running it through (i.e.: Django) such that you can consume the output of the queue processor as it becomes available. No need for polling OR callbacks.

    0 讨论(0)
  • 2021-02-13 16:10

    For your situation I would choose polling. When the second server makes the initial request to create the job on the first server, it should get a response that has the url of the eventual status page. The second server then polls that url every 5-15 minutes to check the status of the job. If the first server makes that url an RSS or Atom feed, then users could also point their RSS readers at the same url and find out themselves if the job is done. It's a real win when both people and machines can get information out of a single source.

    0 讨论(0)
提交回复
热议问题