草庐IT

google-app-engine - Go App Engine 写入 Google Datastore 挂起

coder 2023-06-28 原文

我正在使用 Go 编写一个简单的 App Engine 程序。我需要将一些数据写入 Google DataStore。当我尝试将数据放入存储时,程序一直挂起。这整个星​​期都困扰着我。

在某个时候,我设法将数据写入 DataStore,直到我发现如何在 Cloud Console 中查看上传的数据后才意识到这一点。从那以后我改了很多代码,现在我不能再写任何数据了。它每次都挂起。

日志显示没有任何帮助。仅通知该过程已超时。没有从程序写入的日志,仅从系统写入。

Process terminated because the request deadline was exceeded. (Error code 123)

我尝试使用 this link 更改导入语句, 但编译失败,所以我回到了 this link编译没有问题。

我考虑到我的开发环境可能以某种方式严重错误配置(也许是毁容?),所以我换了一台干净的机器并小心地进行了新安装。它仍然挂起。

我的 app.yaml 文件:

runtime: go
    api_version: go1

handlers:
    - url: /.*
      script: _go_app

我添加的 index.yaml 文件:

indexes:

    - kind: Bacon
        properties:
    - name: YCode
        direction: asc
    - name: URL
    - name: Owner
    - name: Location

这是我的代码:

package main

    import (
        "fmt"
        "log"
        "net/http"
        "google.golang.org/appengine/datastore"
        "google.golang.org/appengine" 
    )

     type Bacon struct {
        YCode string
        URL string
        Owner string
        Location string
    } 

    func main() {
        http.HandleFunc("/", indexHandler)
        appengine.Main() 
    }

    func indexHandler(w http.ResponseWriter, r *http.Request) {

        ctx := appengine.NewContext(r)
        projectID := "fake-for-discussion-thread-787987"

        client, err := datastore.NewClient(ctx, projectID)
        if err != nil {
            log.Fatalf("Failed to create client: %v", err)
            return
        } 

        kind := "Bacon"
        name := "3"
        baconKey := datastore.NameKey(kind, name, nil)

        //Make bacon
        bacon := Bacon{
            YCode: "1",
            URL: "http://www.safeway.com",
            Owner: "Bob",
            Location: "Deli",
        }

        // I've confirmed that this is where it hangs every time.  
        // It doesn't even capture my silly fatal log entry. 
        // It just times out and finally sends a 500 Server Error.
        if _, err := client.Put(ctx, baconKey, &bacon); err != nil {
            log.Fatalf("Failed to save my Bacon: %v", err)
        }

        client.Close()

    }

非常感谢您提供的任何帮助!

最佳答案

好的!所以这真的把我逼到了墙外,但今天早上我设法破解了它。我很难找到在线文档,所以我想确保我输入了我的(现在可以工作的)代码,这样其他人就可以免于痛苦。

值得注意的是,如果您要部署到 App Engine,我开始怀疑 Go 文档/示例可能会让您误入歧途。 App Engine 不喜欢一些核心 Go 库等。在部署到 App Engine 时,这会变得非常困惑。

另请注意,我在最初问题的评论中提出了很多经验教训。我正在创建此条目,以便我可以正确发布我固定的 App Engine 特定代码。

    //Modified to work on App Engine. May not work right locally, etc.

    package main

    import (
        "fmt"
        "log"
        "net/http"
        "google.golang.org/appengine/datastore"
        "google.golang.org/appengine" 
    )

     type Bacon struct {
        YCode string
        URL string
        Owner string
        Location string
    } 

    func main() {
        http.HandleFunc("/", indexHandler)
        appengine.Main() 
    }

    func indexHandler(w http.ResponseWriter, r *http.Request) {

        //This gives a context that makes App Engine happy      
        ctx := appengine.NewContext(r)

        //NOT NEEDED (Apparently, App Engine knows who you are. Creepy.)    
        //projectID := "fake-for-discussion-thread-787987" 

        //NOT NEEDED (And may actually cause you great woe!)
        //client, err := datastore.NewClient(ctx, projectID)
        //if err != nil {
        //  log.Fatalf("Failed to create client: %v", err)
        //  return
        //} 

        //Turns out that these aren't needed, either. 
        //kind := "Bacon"
        //name := "3"

        //Note the changes from my original code, including the
        //change from NameKey to NewIncompleteKey and the
        //hard-coding of Bacon in the arguments. Alternatively, you might
        //use a variable, depending on your needs. I just need bacon.
        //This causes the entity you create in Google DataStore 
        //to have a "Kind" of "Bacon"
        baconKey := datastore.NewIncompleteKey(ctx, "Bacon", nil)

        //Make bacon
        bacon := Bacon{
            YCode: "1",
            URL: "http://www.safeway.com",
            Owner: "Bob",
            Location: "Deli",
        }

        // Note changes here. No more client, just a call to datastore.Put.
        // Now it works!  One caveat: I'm still trying to figure out why it writes 
        // my data twice. But, hey!  At least it's writing!
        if _, err := datastore.Put(ctx, baconKey, &bacon); err != nil {
            log.Fatalf("Failed to save my Bacon: %v", err)
        }

        //No client, so no need to close.
        //client.Close()

    }

干杯!

关于google-app-engine - Go App Engine 写入 Google Datastore 挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50651237/

有关google-app-engine - Go App Engine 写入 Google Datastore 挂起的更多相关文章

  1. Ruby 写入和读取对象到文件 - 2

    好的,所以我的目标是轻松地将一些数据保存到磁盘以备后用。您如何简单地写入然后读取一个对象?所以如果我有一个简单的类classCattr_accessor:a,:bdefinitialize(a,b)@a,@b=a,bendend所以如果我从中非常快地制作一个objobj=C.new("foo","bar")#justgaveitsomerandomvalues然后我可以把它变成一个kindaidstring=obj.to_s#whichreturns""我终于可以将此字符串打印到文件或其他内容中。我的问题是,我该如何再次将这个id变回一个对象?我知道我可以自己挑选信息并制作一个接受该信

  2. ruby - RuntimeError(自动加载常量 Apps 多线程时检测到循环依赖 - 2

    我收到这个错误:RuntimeError(自动加载常量Apps时检测到循环依赖当我使用多线程时。下面是我的代码。为什么会这样?我尝试多线程的原因是因为我正在编写一个HTML抓取应用程序。对Nokogiri::HTML(open())的调用是一个同步阻塞调用,需要1秒才能返回,我有100,000多个页面要访问,所以我试图运行多个线程来解决这个问题。有更好的方法吗?classToolsController0)app.website=array.join(',')putsapp.websiteelseapp.website="NONE"endapp.saveapps=Apps.order("

  3. ruby-on-rails - 每次我尝试部署时,我都会得到 - (gcloud.preview.app.deploy) 错误响应 : [4] DEADLINE_EXCEEDED - 2

    我是Google云的新手,我正在尝试对其进行首次部署。我的第一个部署是RubyonRails项目。我基本上是在关注thisguideinthegoogleclouddocumentation.唯一的区别是我使用的是我自己的项目,而不是他们提供的“helloworld”项目。这是我的app.yaml文件runtime:customvm:trueentrypoint:bundleexecrackup-p8080-Eproductionconfig.ruresources:cpu:0.5memory_gb:1.3disk_size_gb:10当我转到我的项目目录并运行gcloudprevie

  4. ruby-on-rails - 如何重命名或移动 Rails 的 README_FOR_APP - 2

    当我在我的Rails应用程序根目录中运行rakedoc:app时,API文档是使用/doc/README_FOR_APP作为主页生成的。我想向该文件添加.rdoc扩展名,以便它在GitHub上正确呈现。更好的是,我想将它移动到应用程序根目录(/README.rdoc)。有没有办法通过修改包含的rake/rdoctask任务在我的Rakefile中执行此操作?是否有某个地方可以查找可以修改的主页文件的名称?还是我必须编写一个新的Rake任务?额外的问题:Rails应用程序的两个单独文件/README和/doc/README_FOR_APP背后的逻辑是什么?为什么不只有一个?

  5. ruby - 使用 postgres.app 在 rvm 下要求 pg 时出错 - 2

    我正在使用Postgres.app在OSX(10.8.3)上。我已经修改了我的PATH,以便应用程序的bin文件夹位于所有其他文件夹之前。Rammy:~phrogz$whichpg_config/Applications/Postgres.app/Contents/MacOS/bin/pg_config我已经安装了rvm并且可以毫无错误地安装pggem,但是当我需要它时我得到一个错误:Rammy:~phrogz$gem-v1.8.25Rammy:~phrogz$geminstallpgFetching:pg-0.15.1.gem(100%)Buildingnativeextension

  6. ruby - Ruby 是否使用 $stdout 来写入 puts 和 return 的输出? - 2

    我想知道Ruby用来在命令行打印这些东西的输出流:irb(main):001:0>a="test"=>"test"irb(main):002:0>putsatest=>nilirb(main):003:0>a=>"test"$stdout是否用于irb(main):002:0>和irb(main):003:0>?而且,在这两次调用之间,$stdout的值是否有任何变化?另外,有人能告诉我打印/写入这些内容的Ruby源代码吗? 最佳答案 是的。而且很容易向自己测试/证明。在命令行试试这个:ruby-e'puts"foo"'>test.

  7. Ruby:写入 stdin 并从 stdout 读取? - 2

    我正在编写一个ruby​​程序,它应该执行另一个程序,通过stdin向它传递值,从它的stdout读取响应,然后打印响应。这是我目前所拥有的。#!/usr/bin/envrubyrequire'open3'stdin,stdout,stderr=Open3.popen3('./MyProgram')stdin.puts"helloworld!"output=stdout.readerrors=stderr.readstdin.closestdout.closestderr.closeputs"Output:"puts"-------"putsoutputputs"\nErrors:"p

  8. Ruby -> 写入二维数组 - 2

    我正在处理http://prepwork.appacademy.io/mini-curriculum/array/中概述的数组问题我正在尝试创建函数my_transpose,它接受一个矩阵并返回其转置。我对写入二维数组感到很困惑!这是一个代码片段,突出了我的困惑。rows=[[0,1,2],[3,4,5],[6,7,8]]columns=Array.new(3,Array.new(3))putscolumns.to_s#Outputisa3x3arrayfilledwithnilcolumns[0][0]=0putscolumns.to_s#Outputis[[0,nil,nil],[

  9. ruby - Google-api-ruby-client 翻译 API 示例 - 2

    很高兴看到google代码:google-api-ruby-client项目,因为这对我来说意味着Ruby人员可以使用GoogleAPI-s来完善代码。虽然我现在很困惑,因为给出的唯一示例使用Buzz,并且根据我的实验,Google翻译(v2)api的行为必须与google-api-ruby-client中的Buzz完全不同。.我对“Explorer”演示示例很感兴趣——但据我所知,它并不是一个探索器。它所做的只是调用一个Buzz服务,然后浏览它已经知道的关于Buzz服务的事情。对我来说,Explorer应该让您“发现”所公开的服务和方法/功能,而不一定已经知道它们。我很想听听使用这个

  10. ruby - 使写入文件线程安全 - 2

    我在一个ruby​​文件中有一个函数可以像这样写入一个文件File.open("myfile",'a'){|f|f.puts("#{sometext}")}这个函数在不同的线程中被调用,使得像上面这样的文件写入不是线程安全的。有谁知道如何以最简单的方式使这个文件写入线程安全?更多信息:如果重要的话,我正在使用rspec框架。 最佳答案 您可以通过File#flock给锁File.open("myfile",'a'){|f|f.flock(File::LOCK_EX)f.puts("#{sometext}")}

随机推荐