[Ironpython-users] Gradually executing a python script

Keith Rome rome at Wintellect.com
Mon Jul 23 20:12:24 CEST 2012

Turn-based design is to safely address the non-deterministic characteristics of your current design (you said: "I have the problem that the outcome of executing the game gives different results every time"). By making the design turn-based, you can share computation cycles among all of your character AI scripts, without needing to run them on parallel threads. You run each AI for one turn, then cycle through them again for the next turn, etc., all on one thread of execution. The alternative is to introduce thread synchronization code to your parallel AI implementations - which is a far more difficult task, and ultimately cannot be 100% fool-proof.

Using a trace profiler function was suggested as a possible solution to your requirement of controlling line-by-line execution of the scripts (to enforce a memory quota).

These are distinct and separate solutions to the two problems that you were wanting to solve - they are not dependent upon each other.

Also, the IronPython compiler will catch syntax errors for you. It isn't a line interpreter - it compiles an AST from the entire script prior to execution. So you don't have to worry about it encountering syntax errors once execution starts. You can get to those errors if you need, but the script will not be runnable until it compiles cleanly. Runtime errors are different, and they will generate Exceptions that can be caught and handled by the python script code, and if they are unhandled by the script then they will be raised in the hosting runtime environment - ultimately bubbling up to where you made the call to execute the script.

Keith Rome
Senior Consultant and Architect
Wintellect | 770.617.4016 | krome at wintellect.com<mailto:rome at wintellect.com>

From: Jesper Taxbøl [mailto:jesper at taxboel.dk]
Sent: Monday, July 23, 2012 1:49 PM
To: Keith Rome
Cc: Kevin Hazzard; ironpython-users at python.org
Subject: Re: [Ironpython-users] Gradually executing a python script

The reason I want to do it gradually is that I cant be sure that the script ever terminates or has syntax errors. Would that not be a problem in a turn based setup?

2012/7/23 Keith Rome <rome at wintellect.com<mailto:rome at wintellect.com>>
You may want to look into leveraging the sys.settrace() feature of Python for controlling line-by-line execution. This API allows you to install a profiling function that gets invoked for every frame of script code that is executed. In your profiling function, you could compute the amount of memory being used by variables within the ScriptScope, and halt execution if you need to. Just be careful about what you do in your profiling function, as it will be called extremely often by the runtime.


The reason you are seeing variant results is probably due to how you have implemented multithreading. The IronPython runtime is mostly thread-safe (as long as you don't use libraries that make use of mutable objects, and as long as you import all libraries used at least once prior to forking threads). But your code must also be thread-safe as well. From your descriptions of your game engine, it sounds like your game engine is not designed to be thread-safe so I would strongly recommend avoiding multithreading as a means of providing resource sharing. It is very difficult to write 100% thread-safe code, and nothing will stop people from writing unsafe scripts in your game.

Instead, I would suggest implementing your game engine as a turn-based system. For each turn, the script for each character is executed completely. This will allow you to cycle through all characters one turn at a time, equally, and will also eliminate the problem of having variant outcomes since the program will become deterministic.

Keith Rome
Senior Consultant and Architect
Wintellect | 770.617.4016<tel:770.617.4016> | krome at wintellect.com<mailto:rome at wintellect.com>

From: ironpython-users-bounces+rome=wintellect.com at python.org<mailto:wintellect.com at python.org> [mailto:ironpython-users-bounces+rome<mailto:ironpython-users-bounces%2Brome>=wintellect.com at python.org<mailto:wintellect.com at python.org>] On Behalf Of Jesper Taxbøl
Sent: Monday, July 23, 2012 11:05 AM
To: Kevin Hazzard
Cc: ironpython-users at python.org<mailto:ironpython-users at python.org>
Subject: Re: [Ironpython-users] Gradually executing a python script

Would that allow me to step gradually through a loop?


x = 0
while x < 10:
while x > 0:

2012/7/23 Kevin Hazzard <wkhazzard at gmail.com<mailto:wkhazzard at gmail.com>>
Why don't you use a scripting host and inject commands into a ScriptEngine reusing a ScriptScope as you need to execute them?

On Mon, Jul 23, 2012 at 5:31 AM, Jesper Taxbøl <jesper at taxboel.dk<mailto:jesper at taxboel.dk>> wrote:

I am not that familiar with Ironpython yet, but I have a question that I hope you can help me answer.

I am working on a programming-game where I will allow users to do some simple python scripting against a simple API that I will control a game character. Instructions like move and shoot etc, alongside some simple sense functions that return info on the game world.

My current prototype creates an ironpython engine for each game character and executes the script in a thread by itself, which sort of works. But I have the problem that the outcome of executing the game gives different results every time. Therefore I would like to ask the question:

Is it possible to execute a script inside the Ironpython engine gradually?

I imagine that I could update a list of engines with a tick(int cycles) and get a fair sharing of resources between engines and ensure the same result every time.

Kind regards


As this is a programming game I would also like to be able to limit the available memory each script is using. Is there a way to limit this, so a script like this would be killed.

x = 0
v = {}
while True:
   x= x + 1

Ironpython-users mailing list
Ironpython-users at python.org<mailto:Ironpython-users at python.org>

W. Kevin Hazzard
Consultant, Author, Teacher, Microsoft MVP
(804) 928-3444<tel:%28804%29%20928-3444>
book<http://manning.com/hazzard> | mvp<https://mvp.support.microsoft.com/profile/Kevin.Hazzard> | twitter<http://twitter.com/#%21/KevinHazzard> | facebook<http://www.facebook.com/wkhazzard> | linkedin<http://www.linkedin.com/in/kevinhazzard> | captech<http://captechconsulting.com>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20120723/1ca88c1d/attachment.html>

More information about the Ironpython-users mailing list