[Unresolved] Webpy bug when using sessions with generator and normal function handlers

本文探讨了Web.py框架中使用session与generator时遇到的问题。具体表现为,在使用generator作为返回值时,session不能正确更新。文章分析了问题的原因,并提出了可能的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

class count1:
	def GET(self):
		s.session.count += 1
		return str(s.session.count)

class count2:
	def GET(self):
		s.session.count += 1
		yield str(s.session.count)


With above two handlers differs only in the way returning information (the 2nd one being a generator), web.py session actually works with the first one but not the second case.

Simple scenario is:

Web.py uses "processor", similar to Chain-of-Responsibility in GoF patterns, to add processing steps to the handler functions. Session, implemented this way, saves session information automatically when the handler is called and finished:

# session.py L83
    def _processor(self, handler):
        """Application processor to setup session for every request"""
        self._cleanup()
        self._load()

        try:
            return handler()
        finally:
            self._save()

In usual case (or: count1), then handler() calls the whole handler function and return the string as the result, which is fine. And the session object has all update in-place then _save()ed in the finally block.

In the generator (or, count2) case though, the handler() only returns a generator object of count2.GET (bound to some object) but the handler code is not actually started. Therefore, when self._save() is being called, it's actually before the session counter update. Web.py then uses itertools.chain to unroll the generator and get all output to the client, after the handle_with_processors() call in application.py line 279, which is all too late already.

I believe that the correct way is actually unroll the generator before returning from the handler() call (application.py line 239, "return self.handle()"). But this hasn't been verified and I need insights onto this.

Link to issue on GitHub: https://github.com/webpy/webpy/issues/235

Unresolved function or method getone()” 是一种常见的编程错误提示,通常出现在使用集成开发环境(IDE)进行代码编写和调试时。这个错误提示表明编译器无法识别名为“getone”的函数或方法。以下是一些可能的原因和解决方法: ### 可能的原因 1. **函数未定义**:在代码中未定义名为“getone”的函数或方法。 2. **拼写错误**:函数名拼写错误,确保函数名拼写正确。 3. **作用域问题**:函数定义的作用域不正确,可能在调用时不可见。 4. **未导入模块**:如果函数在另一个模块中定义,确保已经正确导入该模块。 5. **类型不匹配**:函数参数类型或返回类型不匹配,导致编译器无法识别。 ### 解决方法 1. **检查函数定义**:确保在代码中正确定义了名为“getone”的函数或方法。 2. **检查拼写**:仔细检查函数名的拼写,确保没有拼写错误。 3. **检查作用域**:确保函数定义在调用它的作用域内。 4. **导入模块**:如果函数在另一个模块中定义,确保已经正确导入该模块。 5. **检查参数和返回类型**:确保函数参数类型和返回类型与调用时匹配。 ### 示例 假设你在使用JavaScript编写代码,遇到了“Unresolved function or method getone()”错误: ```javascript // 定义getone函数 function getone() { return 1; } // 调用getone函数 console.log(getone()); ``` 如果上述代码在正确的文件中,并且没有拼写错误,编译器应该能够正确识别并调用“getone”函数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值