gpt4 book ai didi

http - 如何在 2 个不同的 API 调用之间共享数据?

转载 作者:行者123 更新时间:2023-12-01 22:15:01 24 4
gpt4 key购买 nike

我正在尝试创建一个框架,在该框架中我将通过 REST API 接收请求并等待另一个服务(通过 gRPC 工作)来轮询并执行请求。这是必需的,因为“其他”服务非常深入地嵌入到网络中,我不能直接调用它。同时,我想将其他服务的输出缓冲回请求源。

有什么想法可以在 2 个不同的异步 API 请求之间共享这些数据吗?使用文件系统是一种方式......但我在想我可以通过 channel 或其他方式做得更好......?

最佳答案

下面是一种伪代码:

func RestHandler(payload string) (string, error){
respChan := make(chan string)
workId := placeWorkInQueue(payload)
// Start polling in the background
go pollForResult(respChan, workId)
// wait for result in the channel
var result string
select {
case result = <-respChan:
// implement your timeout logic as a another case: here
}
return result, nil
}

// This is poller for just the workId given to it.
func pollForResult(respChan chan string, workId string) {
// Do the polling for workId result
/// Write response to respChan when available
// You may have to implement a timeout to give up polling.

}

func placeWorkInQueue(s string) string {
// Place the job in queue and return a unique workId
return "unique-id"
}

关于http - 如何在 2 个不同的 API 调用之间共享数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60903965/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com