Django/Python-每秒更新一次数据库 [英] Django/Python - Updating the database every second

查看:54
本文介绍了Django/Python-每秒更新一次数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Django和Python创建基于浏览器的游戏,并且我正试图提出解决方案来解决我遇到的问题之一.

I'm working on creating a browser-based game in Django and Python, and I'm trying to come up with a solution to one of the problems I'm having.

基本上,每秒需要更新多个用户变量.例如,有一个货币变量应该每秒增加一些,随着升级和所有爵士乐而逐渐变大.

Essentially, every second, multiple user variables need to be updated. For example, there's a currency variable that should increase by some amount every second, progressively getting larger as you level-up and all of that jazz.

我觉得用cronjobs做到这一点是一个坏主意(从我的研究中,其他人也这么认为),所以现在我想我应该创建一个线程来循环遍历所有用户.执行这些更新的数据库.

I feel like it's a bad idea to do this with cronjobs (and from my research, other people think that too), so right now I'm thinking I should just create a thread that loops through all of the users in the database that performs these updates.

我在这里的路线正确吗,还是有更好的解决方案?在Django中,如何在服务器启动的第二秒启动线程?

Am I on the right track here, or is there a better solution? In Django, how can I start a thread the second the server starts?

我感谢您的见识.

推荐答案

可能的解决方案之一是使用单独的守护进程轻量级python脚本执行所有游戏内业务逻辑,而django只是游戏的前端.要将它们绑定在一起,您可以选择任何高性能的异步消息传递库,例如ZeroMQ(例如,将播放器的动作传递给该脚本).该堆栈还具有将前端分离并完全不了解后端实现的优点.

One of the possible solutions would be to use separate daemonized lightweight python script to perform all the in-game business logic and left django be just the frontend to your game. To bind them together you might pick any of high-performance asynchronous messaging library like ZeroMQ (for instance to pass player's actions to that script). This stack would also have a benefit of a frontend being separated and completely agnostic of a backend implementation.

这篇关于Django/Python-每秒更新一次数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆