[Twisted-Python] Pyjamas + web2py + twisted vs Django + Pinax

Luke Kenneth Casson Leighton luke.leighton at googlemail.com
Thu Sep 24 16:53:12 EDT 2009


sorry, folks: i didn't see this message, and it's taken me ten months
of obscure searches to find it.

> > I'm about to start programming a game in Python. The game will
> > be a web-based board game with a social aspect, something like
> > http://www.chesspark.com. I want the game to run in both browsers
> > and as a desktop client.

 and presumably you don't want to duplicate effort.  ergo, pyjamas
 is a good choice.  actually... it's your _only_ python choice
 if you don't want to burden the users.

 your other choices are: adobe AIR, silverlight, and, more recently,
 appcelerator.  appcelerator is interesting: you get to run IronPython
 because appcelerator's "titanium" is actually a silverlight / browser
 plugin that gives IronPython apps access to the DOM model of the

> > Therefore I'm wondering what python-based
> > technologies I need. I know that Javascript/AJAX is necessary for
> > the browser part so I have decided on the following:
> >
> > 1. Pyjamas -  (for the AJAX browser stuff)
> >
> > 2. Pyjamas-desktop - (for the desktop client)

 pyjamas-desktop now has XULrunner and MSHTML as back-end engines.
 the MSHTML one is particularly strategically significant, as the
 only real requirements are, instead of a whopping 30mb bundle
 including gtk, cairo, xulrunner, pygtk, pycairo, pygobject _and_
 not to mention python itself, you can just install... python-comtypes
 (which is a 200k download).  for the POSIX-compliant systems, such
 as debian, there's "apt-get install hulahop", and you get python-xpcom,
 xulrunner and all other dependencies pulled in "for free".

 > > 3. Web2py (or djanjo, or django+pinax)  - for the website on
which the game will run and for database access and social part -
login, chat, members, profiles, game history, etc.)
> >
> > 4. Twisted - for the networking and chat functionality.
> >
> > 5. Pygame - for most of the game stuff, sprites, inages, etc. (Actually I don't know if Pygame is necessary for a board-type game.)

 no, it's not.  i wrote a board-type game "meta-engine" which involved
 moving of pyjamas.ui.Image "tokens" around a pyjamas.ui.AbsolutePanel
 and it was incredibly simple and it actually worked.  i went to a lot
 of trouble to use the django server and AJAX (actually JSONRPC) to
 communicate "moving" of tokens.  as one user moved a token, the fact
 that it had "moved" would be noted in a database table
 (id, game_session_id, user_id, token_id)
 of course obviously excluding the user who had moved it.

 a continuous poll by a user of the database would empty the table
 of "moved" notifications for that user, and return a list of new
 position info for the tokens that needed to be moved.

 update speed turned out to be about 1-2 frames per second: perfectly
 adequate for a board game.

 oh: for "Go", due to the number of tokens, however, i found
 that over 700 tokens resulted in _considerably_ slow Firefox 2
 initialisation times, which turns out to be a known bug in Firefox 2.
 adding over 256 absolute-positioned items to a div element using
 DOM manipulation is *SEVERELY* cpu-intensive: beyond some sort of
 fixed limit, adding elements turns from O(N) to O(N^2).  for full information
 on the issue, courtesy of john gunther, and a solution, see:

 i also added "spectators" which were simply "users with no rights
 to move tokens".  they got their own "chat" channel so as not to
 interfere with the players.

 twisted users are probably screaming loudly at the "polling" technique
 especially one that hammers a database so heavily, but hey, you can't
 have everything :)  if anyone knows of any better event-driven methods
 including example code that can be queried with JSONRPC i'm _really_
 interested to hear about it.  think in terms of "don't respond to the
 JSONRPC request unless either 15 seconds has passed, or there are some
 events which have been triggered by other users".

 yes i fully understand the complexities involved and that one possible
 implementation is a single-process asynchronous JSONRPC HTTP service,
 which is _nasty_ as hell: i'm interested to hear if there's anything

> > My understanding of the above technologies is still limited and I'm not sure if I need all of these because some functionality may be overlapped. e.g. I don't know where Pyjamas leaves off and where web2py/danjo/pinax begins.
> >
> > Can someone please give me some advice?
> That's a fine laundry list of frameworks, but i believe getting all of
> them to cooperate would be practically impossible.  (Note: a framework
> is a library that's grown to the point where it thinks it owns your
> whole program.  Getting multiple frameworks to play nicely together is
> usually hard.  There are rare exceptions, like the beautiful integration
> between Twisted and PyGTK, but they're rare.)

 the integration point of choice between pyjamas and frameworks is JSONRPC,
 simply because it was the first one that was implemented, and it's turned
 out to be incredibly practical.

 examination of the wiki http://code.google.com/p/pyjamas/wiki shows
 over 90% of the wiki pages are "how to use pyjamas with <insert framework>".
 including two or three methods for twisted, and two or more with django.

> I know that Django and pygame and Twisted are useful, mature, documented
> frameworks that actually get used in production by people other than
> their authors.  I don't know about Pyjamas.

 http://pyjs.org/FAQ.html "i'm a bit underwhelmed by the apparent lack
 of documentation": says "go back to main site, http://pyjs.org, see
 Documentation section" which has these:

 still relevant, because the U.I API hasn't really changed:
 for best results, read:
 specifically the U.I "package":

> If putting 100% Python in one end and getting Ajax out the other is
> really important to you, then you need Pyjamas to work.  (Or wait for
> PyPy and its JavaScript backend to mature from experimental research
> code to production-quality stuff.)

 that's not going to happen.  the pypy javascript back-end has been
 abandoned.  it's still available in a branch, but you are not going
 to get anywhere with it.  plus, due to the fact that the sole and
 exclusive focus of the pypy javascript back-end is to be totally
 100% python interoperable, to the absolute exclusion of all else
 (e.g. speed), even if you got it going, you'd find that apps ran
 like treacle.  then, if you tried to debug the app, you'd find that
 it was near-impossible, because the code is near-pure-assembler-like,
 having gone through rpython, first.  being utterly divorced from its
 original python, you are entirely at the mercy of the pypy developers
 for bug-fixes aaand... they've abandoned it.

 bottom line: forget pypy.

> So my advice is to write a simple
> tic-tac-toe game in Pyjamas, and find out.

 or a lightout game.  http://pyjs.org/examples/lightout/output/lightout.html
 or a sudoku game. http://lkcl.net/sudoku/output/Sudoku.html
 (in each case, full source code is 1 level below)

 of course, these are single-user games.  what you should be slightly
 surprised by is the fact that the source code of each app is so damn
 small.  the sudoku one, it hardly looks like there's any actual
 "pyjamas" code in it at all.  you have to _really_ peer very hard
 at the code, looking for clues such as "ListBox", "Grid", "HTML"
 and "HorizontalPanel" in order to discern the bits that just get
 on with serving up a sudoku game and the bits which "glorify pyjamas"
 so to speak.  "oh look, there's a button.  i wonder if it calls
 this funny method named 'onClick' if the button gets clicked.  why
 yes, what a surprise, it does!"

 anyway this is a bit long, i'll stop now :)


More information about the Twisted-Python mailing list