我有两个服务器:Golang和Python(2.7)。Python(Bottle)服务器需要执行一个计算密集型任务,并公开一个restfuluri来启动进程的执行。也就是说,Go服务器发送:
HTTP GET to myserver.com/data
python服务器执行计算,并需要通知Go服务器处理的完成。我认为有两种方法可以设计:
Go向Python发送一个回调URL/数据,Python通过点击该URL进行响应。E、 g:
HTTP获取|myserver.com/data|数据{callbackURI:goserver.com/process/results,类型:POST,响应:“processComplete”}
让一个基于WebSocket的响应从Python发回。
什么是更合适的设计?两者孰优孰劣?除了错误情况(服务器崩溃等),Python服务器需要真正“通知”客户机的唯一事情就是完成计算。这是唯一的回应。在
在Go服务器上工作的团队不太熟悉基于websockets/ajax的Go客户端(我也不熟悉,但我从来没有写过一行Go:)#1似乎更容易,但不知道这是一种公认的设计方法还是仅仅是一种黑客攻击?在这方面有什么建议?在
如果您想实现RESTful,那么当客户机请求} 状态代码:
HTTP GET myserver.com/data
时,服务器应该返回一个^{Python服务器可以向临时资源返回ETA和URL,以请求操作的当前状态(例如:
myserver.com/temp_data?processing_status
)。然后由Go客户端通过请求此资源并读取ETA来等待任务完成。一旦处理完成,Python服务器就可以返回一个410 Gone
状态和新资源的最终URL。在这取决于这些信号被发送的频率。如果是每秒多次,保持websocket打开可能更有意义。否则,使用选项#1,因为它的开销更小,耦合更松散。在
相关问题 更多 >
编程相关推荐