examples of CGI "games" with Python

Erik Max Francis max at alcyone.com
Fri Dec 13 17:52:27 EST 2002


Will Stuyvesant wrote:

> And do you have any ideas about a good design for it?  There is a
> design problem with the gamelogic (server side) that does a turn say
> every 30 seconds.  Users can send commands via CGI, commands get
> queued up till a maximum, 10 or so.  I am not comfortable with unix,
> should I start a background job that takes the turns, write data to
> files?  I read something about ``crontab`` but I have no idea what it
> is and my access to the machine that provides the CGI is limited.
> A completely different idea is to have an application that does a
> CGI-nextturn request every 30 seconds...of course this has to be
> hidden from ordinary users :-)

There are really two ways to approach this.  The first is to use pure
CGI and a database running somewhere -- or a set of state files
somewhere with a carefully managed lockfiles system if a real database
is not available -- and have each invocation of the CGI script read in
the parameters it needs from the submitted form (if any), access the
portions of the database it needs, put them into some appropriate Python
data structures for the purposes of answering the request, and display a
response page (presumably also including forms).  Since you want regular
turn updates, then you'd put a script in a cronjob which would do the
appropriate turn updates.  In both cases, the relevant state of the
database needs to be maintained, since CGI requests can hit the server
asynchronously.  Particularly if you're doing this manually with state
files (I'm guessing from your description you probably don't have access
to a real database to do the dirty work for you), this requires careful
handling of lockfiles to prevent potential corruption.

The second approach, which I would recommend, is to simply write the
HTTP server yourself.  This is pretty easy in Python even if you want to
do it from the ground up, but there already exist numerous frameworks to
assist you with this, e.g., Medusa or Twisted.

For Medusa:

	http://www.nightmare.com/medusa/index.html
	http://www.amk.ca/python/code/medusa.html

or Twisted:

	http://www.twistedmatrix.com/

Python's pickling facilities make serialization an easy task, so you
practically get a stateful server for free.  I would recommend an
asynchronous approach, rather than threads.  (This has the benefit of
when you do a turn update, things "just work" -- the server doesn't
process new CGI requests in the brief time it's doing a turn update, so
the issue of conflicts of receiving orders during a turn update simply
do not exist.)

There are lots of details to cover, of course, but that's the basic
sketch.

> An *impressive* list !
> Do you know if any of them use Python?

Not that I know of, but I'm sure there are a few here and there.

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE
/  \ Triumph cannot help being cruel.
\__/ Jose Ortega y Gasset
    Max Pandaemonium / http://www.maxpandaemonium.com/
 A sampling of Max Pandameonium's music.



More information about the Python-list mailing list