App Engine:调试和运行不同(神秘)

时间:2015-10-14 22:05:50

标签: python-2.7 google-app-engine

我正在构建一个wiki并且在调试期间的结果与程序运行期间的结果不同时遇到问题。

class WikiPage(Handler):
    def get(self, id):
            id, existing_article, article_content = self.get_stripped_id_article_content(id)
            logging.error(("GET! id: {0}; article: {1}; content: {2}").format(id, existing_article, article_content))
            <...>

    def strip_id(self, id):
            id = id.replace("/", "")
            return id

    def get_stripped_id_article_content(self, id):
        id = self.strip_id(id)
        q = Article.all()
        q.filter("id = ", id)
        existing_article = q.get()

        content = existing_article.content if existing_article else ""

        return id, existing_article, content
    <...>

class CreateEditPage(WikiPage):
    def post(self, id):
        id, existing_article, article_content = self.get_stripped_id_article_content(id)
        user = self.get_user(self.request)

        input_content = self.request.get("content")

        if existing_article:
            existing_article.content = input_content
            existing_article.put()

        else:            
            new_article = Article(id = id, content = input_content)
            new_article.put()
            id, existing_article, article_content = self.get_stripped_id_article_content(id)
            logging.error(("POST! In Else. id: {0}; article: {1}; content: {2}").format(id, existing_article, article_content))



        id, existing_article, article_content = self.get_stripped_id_article_content(id)
        logging.error(("POST! Outside Else. id: {0}; article: {1}; content: {2}").format(id, existing_article, article_content))
        self.redirect("/" + str(id))

PAGE_RE = r'(/(?:[a-zA-Z0-9_-]+/?)*)'
app = webapp2.WSGIApplication([('/_edit' + PAGE_RE, CreateEditPage),
                               (PAGE_RE, WikiPage),
                               ],
                              debug=True)

首先,我清除了数据库中的所有内容,然后发布了内存缓存。

我运行我的应用程序:

INFO     2015-10-14 21:06:52,744 sdk_update_checker.py:229] Checking for updates to the SDK.
INFO     2015-10-14 21:06:53,135 api_server.py:205] Starting API server at: http://localhost:53588
INFO     2015-10-14 21:06:53,141 dispatcher.py:197] Starting module "default" running at: http://localhost:8080
INFO     2015-10-14 21:06:53,142 admin_server.py:118] Starting admin server at: http://localhost:8000
ERROR    2015-10-14 21:10:10,804 gmv_wiki.py:194] GET! id: ; article: None; content: 
INFO     2015-10-14 21:10:10,810 module.py:809] default: "GET / HTTP/1.1" 302 -
INFO     2015-10-14 21:10:10,847 module.py:809] default: "GET /_edit/ HTTP/1.1" 200 348
ERROR    2015-10-14 21:10:12,228 gmv_wiki.py:194] GET! id: newpost; article: None; content: 
INFO     2015-10-14 21:10:12,239 module.py:809] default: "GET /newpost HTTP/1.1" 302 -
INFO     2015-10-14 21:10:12,264 module.py:809] default: "GET /_edit/newpost HTTP/1.1" 200 348
ERROR    2015-10-14 21:10:18,945 gmv_wiki.py:194] GET! id: new_article; article: None; content: 
INFO     2015-10-14 21:10:18,951 module.py:809] default: "GET /new_article HTTP/1.1" 302 -
INFO     2015-10-14 21:10:18,976 module.py:809] default: "GET /_edit/new_article HTTP/1.1" 200 348
ERROR    2015-10-14 21:10:50,979 gmv_wiki.py:246] POST! In Else. id: new_article; article: None; content: 
ERROR    2015-10-14 21:10:50,985 gmv_wiki.py:251] POST! Outside Else. id: new_article; article: None; content: 
INFO     2015-10-14 21:10:50,991 module.py:809] default: "POST /_edit/new_article HTTP/1.1" 302 -
ERROR    2015-10-14 21:10:51,014 gmv_wiki.py:194] GET! id: new_article; article: None; content: 
INFO     2015-10-14 21:10:51,022 module.py:809] default: "GET /new_article HTTP/1.1" 302 -
INFO     2015-10-14 21:10:51,051 module.py:809] default: "GET /_edit/new_article HTTP/1.1" 200 348
ERROR    2015-10-14 21:11:18,321 gmv_wiki.py:251] POST! Outside Else. id: new_article; article: <gmv_wiki.Article object at 0x7f684d2ba250>; content: New article content.
INFO     2015-10-14 21:11:18,326 module.py:809] default: "POST /_edit/new_article HTTP/1.1" 302 -
ERROR    2015-10-14 21:11:18,351 gmv_wiki.py:194] GET! id: new_article; article: <gmv_wiki.Article object at 0x7f684d2663d0>; content: New article content.
INFO     2015-10-14 21:11:18,358 module.py:809] default: "GET /new_article HTTP/1.1" 200 262

我调试我的应用程序:

pydev debugger: starting (pid: 10230)
INFO     2015-10-14 21:12:24,730 sdk_update_checker.py:229] Checking for updates to the SDK.
INFO     2015-10-14 21:12:25,435 api_server.py:205] Starting API server at: http://localhost:44302
INFO     2015-10-14 21:12:25,463 dispatcher.py:197] Starting module "default" running at: http://localhost:8080
INFO     2015-10-14 21:12:25,467 admin_server.py:118] Starting admin server at: http://localhost:8000
pydev debugger: starting (pid: 10250)
ERROR    2015-10-14 21:12:36,523 gmv_wiki.py:194] GET! id: new_article_1; article: None; content: 
INFO     2015-10-14 21:12:36,547 module.py:809] default: "GET /new_article_1 HTTP/1.1" 302 -
INFO     2015-10-14 21:12:36,766 module.py:809] default: "GET /_edit/new_article_1 HTTP/1.1" 200 348
ERROR    2015-10-14 21:12:54,394 gmv_wiki.py:246] POST! In Else. id: new_article_1; article: None; content: 
ERROR    2015-10-14 21:12:54,430 gmv_wiki.py:251] POST! Outside Else. id: new_article_1; article: None; content: 
INFO     2015-10-14 21:12:54,452 module.py:809] default: "POST /_edit/new_article_1 HTTP/1.1" 302 -
ERROR    2015-10-14 21:12:54,587 gmv_wiki.py:194] GET! id: new_article_1; article: <gmv_wiki.Article object at 0x7fad181bef10>; content: New article 1 content.
INFO     2015-10-14 21:12:54,610 module.py:809] default: "GET /new_article_1 HTTP/1.1" 200 266

您可以查看代码中的logging.error案例吗?并在日志中得到结果。

两种绝对平等的情况。 http://localhost:8080/new_article#普通跑 http://localhost:8080/new_article_1 #debug

该计划的行为如下: 1.在调试期间一切运行良好:创建新文章的编辑表单,当我按“提交”时,内容被放置到数据库,我被重定向到该文章的维基页面。 2.当我刚运行程序(不调试它)时,打开新文章的编辑表单,输入内容并按“提交”。然后我被重定向到维基页面。在该地址没有找到维基页面。我再次被重定向到编辑页面。然后当我再次输入内容时,内容最终被放入数据库并打开维基页面。

对我来说,奥秘是: 1.为什么运行和调试之间存在差异? 2.为什么在我完成new_article.put()后,我仍然没有在post函数中找到文章对象? 然后我被重定向到维基页面,并以某种方式找到文章对象。奇怪。 4.为什么existing_article.put()运行良好而new_article.put()不向数据库放置任何内容?

好吧,我似乎在我的智慧结束。你能帮我解决这些问题吗?谢谢你的建议。

1 个答案:

答案 0 :(得分:1)

我怀疑您遇到了GAE数据存储eventual consistency问题。基本上,在Article.all()查询中找不到保存在new_article.put()中的新文章(通常为短)。您仍然可以通过ID访问它,但是您必须为此调整代码。

我怀疑行为上的差异是由调试模式执行的总体速度较低引起的,这可能会隐藏最终的一致性问题。