我有一个使用Python编写的Flask Web服务器,它会在接收每个请求时执行一些耗费资源的任务。例如:
1) CLIENT --> REQUEST1 --> SERVER
2) Server performing expensive task for request1...
3) CLIENT --> REQUEST2 --> SERVER
4) Server Performing expensive tasks for request1 and request2 in parallel...
5) Server sends complete response for request1 (or request2)
6) Server sends complete response for request2 (or request1)
我希望如果有第二个请求到达,服务器能够停止执行第一个请求(request1),以下是我所需的服务器行为:
1) CLIENT --> REQUEST1 --> SERVER
2) Server performing expensive tasks for request1...
3) CLIENT --> REQUEST2 --> SERVER
4) Server immediately sends (incomplete) response for request1
5) Server performing expensive tasks for request2...
6) Server sends complete response for request2
如何在request2 worker中获取对request1 worker的引用,以便向其发送停止消息? 在flask中是否可以实现这一点(或者必要时利用一些实用程序库)?