开发者

Should you hold a HTTP request while the server performs a time consuming operation? Or let the request go?

开发者 https://www.devze.com 2023-04-08 04:51 出处:网络
Lets say an operation could take 2 to 10 minutes on the server to perform, for example to update an index which is time consuming.

Lets say an operation could take 2 to 10 minutes on the server to perform, for example to update an index which is time consuming.

Would you perform the operation while holding the request? ie not sending a HTTP Response until the operation has finished? And if the client/browser drops the request, just continue with the operation anyway?

What is the alternative? To kick-off the operation, and response with "l开发者_Python百科ong-time operation kicked off" ? What if the operation fails mid-way how would the client know? Maintain a server-side "status" of the operation?

Thanks


You might also use a chunked response. First, push a chunk with some code that would display the “please wait” screen, flush the response and start the work. Then you can either push and flush chunks with periodical progress updates or just push one at the end with a “completed” information. Obviously you can employ JavaScript to get a nice UI.

(The above does not apply if you're using WSGI as the first WSGI specification is written in a way that blocks using responses of unknown length so using chunked responses there is impossible.)


For requests that you know will take a long time (more than a few seconds) to process, you must assume at minimum that the connection may be forcibly severed by a firewall.

I would say you should implement a queue system on the backend. The request to perform the operation becomes a request to queue the operation. When the operation is actually completed, you can either wait for the client to poll, or proactively notify them somehow. For a browser, you'll pretty much have to either poll or send an e-mail.


Would you perform the operation while holding the request? ie not sending a HTTP Response until the operation has finished?

What do your users expect?

And if the client/browser drops the request, just continue with the operation anyway?

What do your users expect?

What is the alternative? To kick-off the operation, and response with "long-time operation kicked off" ?

What else could you possibly do? Ignore the request? Pretend it didn't happen? Obviously, you inform them.

What if the operation fails mid-way how would the client know? Maintain a server-side "status" of the operation?

What's the alternative to that? Magic? Intuition? Obviously, you must retain status. And display the status.

Read up on celery.

0

精彩评论

暂无评论...
验证码 换一张
取 消