gpt4 book ai didi

google-app-engine - 如何从 appengine.Context 创建云 context.Context

转载 作者:IT王子 更新时间:2023-10-29 01:19:31 31 4
gpt4 key购买 nike

如果我有 appengine.Context 而不是 ,我不知道如何调用 cloud.WithContextgoogle.DefaultClient >上下文。上下文

有(旧的)“appengine”和(新的)“google.golang.org/appengine”包。第一个带来自定义 appengine.Context 而第二个带来来自 "golang.org/x/net/context"context.Context/p>

整个 google.golang.org/cloud 只需要 context.Context

我很乐意转向使用新的 "google.golang.org/appengine",但我一直坚持使用尚未移植的 runtime.RunInBackground然而。来自 https://github.com/golang/appengine :

appengine/aetest, appengine/cloudsql and appengine/runtime have not been ported yet.

如果 appengine/runtime 已经被移植,我可以写什么:

import (
"golang.org/x/net/context"

"google.golang.org/appengine"
"google.golang.org/appengine/runtime"
"google.golang.org/cloud"
"google.golang.org/cloud/storage"
)

func handler(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
runtime.RunInBackground(c, func(ctx context.Context) {
hc, _ := google.DefaultClient(ctx, storage.ScopeFullControl)
cc := cloud.WithContext(ctx, appengine.AppID(ctx), hc)

})
}

但是还没有"google.golang.org/appengine/runtime"。所以我有

runtime.RunInBackground(c, func(ctx appengine.Context) {

最佳答案

目前无法从(旧)appengine.Context 获取云 context.Context

托管虚拟机解决方案

一个月前 BackgroundContext() 方法 was addedgoogle.golang.org/appengine(仅适用于托管 VM)。这允许启动 goroutine 并获取云 context.Context 而无需将其传入。

经典 Appengine 的解决方案

目前没有解决方案。

关于google-app-engine - 如何从 appengine.Context 创建云 context.Context,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32164223/

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