[Ironpython-users] Gradually executing a python script

Keith Rome rome at Wintellect.com
Mon Jul 23 17:33:34 CEST 2012


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.

http://docs.python.org/library/sys.html#sys.settrace


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
MCPD-EAD, MCSD, MCDBA, MCTS-WPF, MCTS-TFS, MCTS-WSS
Wintellect | 770.617.4016 | krome at wintellect.com<mailto:rome at wintellect.com>
www.wintellect.com<http://www.wintellect.com/>

From: ironpython-users-bounces+rome=wintellect.com at python.org [mailto:ironpython-users-bounces+rome=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
Subject: Re: [Ironpython-users] Gradually executing a python script

Would that allow me to step gradually through a loop?

like:

x = 0
while x < 10:
   dostuff()
   x=x+1
while x > 0:
   dootherstuff()
   x=x-1



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?

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

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

Tax


P.S:
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:
   v[x]=x
   x= x + 1



_______________________________________________
Ironpython-users mailing list
Ironpython-users at python.org<mailto:Ironpython-users at python.org>
http://mail.python.org/mailman/listinfo/ironpython-users



--
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/#!/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/605f1f79/attachment.html>


More information about the Ironpython-users mailing list