HTTP回调URL与WebSocket的同步响应?

2024-09-30 06:34:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个服务器:Golang和Python(2.7)。Python(Bottle)服务器需要执行一个计算密集型任务,并公开一个restfuluri来启动进程的执行。也就是说,Go服务器发送:

HTTP GET to myserver.com/data

python服务器执行计算,并需要通知Go服务器处理的完成。我认为有两种方法可以设计:

  1. Go向Python发送一个回调URL/数据,Python通过点击该URL进行响应。E、 g:

    HTTP获取|myserver.com/data|数据{callbackURI:goserver.com/process/results,类型:POST,响应:“processComplete”}

  2. 让一个基于WebSocket的响应从Python发回。

什么是更合适的设计?两者孰优孰劣?除了错误情况(服务器崩溃等),Python服务器需要真正“通知”客户机的唯一事情就是完成计算。这是唯一的回应。在

在Go服务器上工作的团队不太熟悉基于websockets/ajax的Go客户端(我也不熟悉,但我从来没有写过一行Go:)#1似乎更容易,但不知道这是一种公认的设计方法还是仅仅是一种黑客攻击?在这方面有什么建议?在


Tags: 数据方法服务器comhttpurlgobottle
2条回答

如果您想实现RESTful,那么当客户机请求HTTP GET myserver.com/data时,服务器应该返回一个^{}状态代码:

202 Accepted

The request has been accepted for processing, but the processing has not been completed. The request might or might not eventually be acted upon, as it might be disallowed when processing actually takes place. There is no facility for re-sending a status code from an asynchronous operation such as this.

The 202 response is intentionally non-committal. Its purpose is to allow a server to accept a request for some other process (perhaps a batch-oriented process that is only run once per day) without requiring that the user agent's connection to the server persist until the process is completed. The entity returned with this response SHOULD include an indication of the request's current status and either a pointer to a status monitor or some estimate of when the user can expect the request to be fulfilled.

Python服务器可以向临时资源返回ETA和URL,以请求操作的当前状态(例如:myserver.com/temp_data?processing_status)。然后由Go客户端通过请求此资源并读取ETA来等待任务完成。一旦处理完成,Python服务器就可以返回一个410 Gone状态和新资源的最终URL。在

这取决于这些信号被发送的频率。如果是每秒多次,保持websocket打开可能更有意义。否则,使用选项#1,因为它的开销更小,耦合更松散。在

相关问题 更多 >

    热门问题