web2py 中跨会话的全局对象访问?
我是 webdev 的新手,这些天计划用 web2py 编写一个游戏。
当开始编码时,我发现我不能轻松地使用全局变量。
出于演示目的,我想要一个可供许多玩家访问的 python 列表对象,我使用 sqlite 来使其工作。
数据库(使用DAL('sqlite:memory:')
不起作用),所以我尝试文件方式,它按照我的想法工作:
memdb = DAL('sqlite://storage.sqlite')
memdb.define_table('room', Field('card_on_desk', 'blob'))
创建一个房间:
roomid = memdb.room.insert(card_on_desk=pickle.dumps(list()))
memdb.commit()
更改房间的card_on_desk字段:
record = memdb.room(roomid)
cards = pickle.loads(record.card_on_desk)
cards.append(','.join(c))
memdb(memdb.room.id==roomid).update(card_on_desk=pickle.dumps(cards))
memdb.commit()
一些大师说有一个cache.ram()方法,我想知道如何做上面的事情。
等待您的答复。
S.Lott 提到我没有描述使用 DAL('sqlite:memory:') 时会发生什么:
使用sqlite:memory
而不是“sqlite://storage.sqlite”,memdb 插入在 http 请求中没问题,但每次调用时都没有保存任何内容memdb.room.insert() 返回我调用的时间,当请求完成时,内存中没有保存任何内容,memdb.room(1) 始终返回 None。
Anthony的建议更有用,我会尝试使用这种方式,但这样做有点困难。
I'm new to webdev, these days plan for writing a boradgame with web2py.
When start to coding, I found that I can't use global variables easily.
for demo purpose, I want a python list object access by many players, I use sqlite to make it work.
the database, (using DAL('sqlite:memory:')
won't work), so I try the file way, it works as my thought:
memdb = DAL('sqlite://storage.sqlite')
memdb.define_table('room', Field('card_on_desk', 'blob'))
create a room:
roomid = memdb.room.insert(card_on_desk=pickle.dumps(list()))
memdb.commit()
change the room's card_on_desk field:
record = memdb.room(roomid)
cards = pickle.loads(record.card_on_desk)
cards.append(','.join(c))
memdb(memdb.room.id==roomid).update(card_on_desk=pickle.dumps(cards))
memdb.commit()
Some gurus said there is a cache.ram() way, I want to know how to do the above stuff.
wait for your answers.
S.Lott mention that I did not describe what happens when using DAL('sqlite:memory:'):
using sqlite:memory
instead of 'sqlite://storage.sqlite', the memdb insert is alright in a http request, but nothing saves, everytime you I call memdb.room.insert()
returns the times I called and when the request is finish, nothing saved in memory, memdb.room(1) always return None.
Anthony's advice is more useful, I'll try to use that way, bit hard to do so.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果你不需要保存信息,那么你可以使用会话,
如果你需要保存并且你想用缓存加速,那么你可以缓存选择:
If you do not need to persist the information, then you can use the session
if you need to persist and you want to speed up things with cache, then you can cache the select: