简体   繁体   English

在Twisted和Django之间共享数据库

[英]Sharing a database between Twisted and Django

I am developing a multiplayer gaming server that uses Django for the webserver (HTML frontend, user authentication, games available, leaderboard, etc.) and Twisted to handle connections between the players and the games and to interface with the games themselves. 我正在开发一个多人游戏服务器,它使用Django作为网络服务器(HTML前端,用户身份验证,游戏可用,排行榜等)和Twisted来处理玩家和游戏之间的连接以及与游戏本身的接口。 The gameserver, the webserver, and the database may run on different machines. 游戏服务器,网络服务器和数据库可以在不同的机器上运行。

What is the "best" way to architect the shared database, in a manner that supports changes to the database schema going forward. 构建共享数据库的“最佳”方式是什么,支持对数据库模式的更改。 Should I try incorporating Django's ORM in the Twisted framework and used deferreds to make it non-blocking? 我应该尝试在Twisted框架中加入Django的ORM并使用延迟来使其无阻塞吗? Should I be stuck creating and maintaining two separate databases schemas / interfaces, one in Django's model and the other using twisted.enterprise.row? 我是否应该坚持创建和维护两个独立的数据库模式/接口,一个在Django的模型中,另一个在使用twisted.enterprise.row?

Similarly, with user authentication, should I utilize twisted's user authentication functionality, or try to include Django modules into the gameserver to handle user authentication on the game side? 同样,使用用户身份验证,我应该使用twisted的用户身份验证功能,还是尝试将Django模块包含在游戏服务器中以处理游戏端的用户身份验证?

First of all I'd identify why you need both Django and Twisted. 首先,我要确定为什么你需要Django和Twisted。 Assuming you are comfortable with Twisted using twisted.web and auth will easily be sufficient and you'll be able to reuse your database layer for both the frontend and backend apps. 假设你习惯使用twisted.web和auth很容易就足够了,你将能够为前端和后端应用重用你的数据库层。

Alternatively you could look at it the other way, what is Twisted doing better as a game server? 或者你可以用另一种方式来看待它,Twisted在游戏服务器上做得更好? Are you hoping to support more players (more simultaneous connections) or something else? 您是否希望支持更多玩家(更多同时连接)或其他什么? Consider that if you must use threaded within twisted to do blocking database access that you are most likely not going to be able to efficently/reliably support hundreds of simultaneous threads. 考虑一下,如果必须使用twisted内的线程来阻止数据库访问,那么您很可能无法有效/可靠地支持数百个并发线程。 Remember python has a Global Interpreter Lock so threads are not necessarily the best way to scale. 记住python有一个Global Interpreter Lock,因此线程不一定是最好的扩展方式。

You should also consider why you are looking to use a SQL Database and an ORM. 您还应该考虑为什么要使用SQL数据库和ORM。 Does your game have data that is really best suited to being stored in an relational database? 您的游戏是否具有最适合存储在关系数据库中的数据? Perhaps it's worth examining something like MongoDB or another key-value or object database for storing game state. 也许值得研究像MongoDB或其他键值或对象数据库来存储游戏状态。 Many of these NoSQL stores have both blocking drivers for use in Django and non-blocking drivers for use in Twisted (txmongo for example). 许多这些NoSQL商店都有用于Django的阻塞驱动程序和用于Twisted的非阻塞驱动程序(例如txmongo)。

That said, if you're dead set on using both Django and Twisted there are a few techniques for embedding blocking DB access into a non-blocking Twisted server. 也就是说,如果您已经开始使用Django和Twisted,那么有一些技术可以将阻塞数据库访问嵌入到非阻塞的Twisted服务器中。

  1. adbapi (uses twisted thread pool) adbapi(使用扭曲的线程池)
  2. Direct use of the twisted thread pool using reactor.deferToThread 使用reactor.deferToThread直接使用扭曲的线程池
  3. The Storm ORM has a branch providing Twisted support (it handles deferToThread calls internally) Storm ORM有一个提供Twisted支持的分支(它在内部处理deferToThread调用)
  4. SAsync is a library that tries to make SQLAlchemy work in an Async way SAsync是一个试图使SQLAlchemy以异步方式工作的库
  5. Have twisted interact via RPC with a process that manages the blocking DB 通过RPC与管理阻塞DB的进程进行交互

So you should be able to manage the Django ORM objects yourself by importing them in twisted and being very careful making calls to reactor.deferToThread. 因此,您应该能够通过扭曲导入Django ORM对象并且非常小心地调用reactor.deferToThread来管理Django ORM对象。 There are many possible issues when working with these objects within twisted in that some ORM objects can issue SQL when accessing/setting a property, etc. 在扭曲中使用这些对象时存在许多可能的问题,因为某些ORM对象在访问/设置属性时可以发出SQL等。

I realize this isn't necessarily the answer you were expecting but perhaps more detail about what you're hoping to accomplish and why you are choosing these specific technologies will allow folks to get you better answers. 我意识到这不一定是你所期待的答案,但也许更多关于你希望完成什么以及为什么选择这些特定技术的细节将让人们得到更好的答案。

I would just avoid the Django ORM, it's not all that and it would be a pain to access outside of a Django context (witness the work that was required to make Django support multiple databases). 我只是避免使用Django ORM,它不是全部,在Django上下文之外访问会很痛苦(见证了Django支持多个数据库所需的工作)。 Twisted database access always requires threads (even with twisted.adbapi), and threads give you access to any ORM you choose. Twisted数据库访问总是需要线程(即使使用twisted.adbapi),并且线程允许您访问您选择的任何ORM。 SQLalchemy would be a good choice. SQLalchemy将是一个不错的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM