Python share CPU time?

neokosmos at gmail.com neokosmos at gmail.com
Thu Aug 10 23:06:32 EDT 2006


Yannick wrote:

> I would like to program a small game in Python, kind of like robocode
> (http://robocode.sourceforge.net/).
> Problem is that I would have to share the CPU between all the robots,
> and thus allocate a time period to each robot. However I couldn't find
> any way to start a thread (robot), and interrupt it after a given time
> period.
> Any suggestions on how to proceed?

You've gotten a number of other suggestions from other posters.  Let me
suggest, though, as someone else has already beat me to, that
programming the robots in Python might not be ideal.  Without some
significant work, you won't be able to prevent one of your "robots"
from tying up your Python interpreter for arbitrary lengths of time.
Consider, for example, a robot that executes the following line:

x = 10 ** 10 ** 10

Now, if you're careful, you *can* prevent this sort of thing.  My guess
is, however, for a "small game," this effort is not going to be worth
it.

> Is Python just not adapted to this kind of things?

That depends.  I think Python would be a good choice of language to
implement a custom VM for such a game, where you could have the level
of control you need.  Now, implementing a simple and limited-purpose VM
really isn't as hard as it sounds.  The hard part comes if you want to
program your VM in anything other than its equivalent of Assembler.
Then, you basically need to write a Language X -> VM compiler for
whatever Language X you'd really like to program in, and that's
generally not a trivial task.




More information about the Python-list mailing list