[IronPython] Suspending, saving script execution?

Hogg, Jonathan Jonathan.Hogg at kbcaim.com
Mon Jan 29 01:01:39 PST 2007


Stackless Python is definitely the way to go, but if you needed to do
this in IronPython/.NET, you can get a poor man's form of
micro-threading with generators. Taking your example, you could re-write
it like so:
 
def main(self):
    yield move(5)
    yield turn(90)
    if self.direction == Direction.East:
        yield turn(180)
    yield move(2)

where 'move' and 'turn' are type constructors - or factory functions or
whatever - that return an object representing the action to be taken.
Now you instantiate the generator to create your micro-thread, and call
'.next()' on it to execute it up to the next yield.

t1 = mytank.main()
action = t1.next()

The difference between this and full threading is that it's cooperative
and that you have to write your own micro-thread scheduler. Presumably
you'll have some kind of game-state/event engine that will keep track of
what each of the actors is currently doing and when they become eligible
for execution again (it's finished moving/turning), you would poke them
to see what they want to do next.

Main problem is that if a piece of user-generated logic is badly behaved
(doesn't yield), then your game would hang.

Jonathan

________________________________

From: users-bounces at lists.ironpython.com
[mailto:users-bounces at lists.ironpython.com] On Behalf Of Curt
Hagenlocher
Sent: 28 January 2007 17:37
To: Discussion of IronPython
Subject: Re: [IronPython] Suspending, saving script execution?


On 1/28/07, Erzengel des Lichtes <erzengel-von-licht at cox.net> wrote: 

	Now, the script is going to need to be suspended while it's
moving 5 meters
	(it's not going to teleport) forward, then again while it's
turning 90 
	degrees to the right, possibly again when it turns around, and
finally once
	again while moving forward 2 meters.
	I can't block the script without suspending the thread/fiber,
right? But
	with a large number of scriptable units, the system will not be
able to cope 
	with a thread/fiber per script.

 
This sounds like the sort of thing that Stackless Python[1] was invented
for.  This is a variation of CPython that -- by removing the dependency
of Python code execution on the processor's stack -- allows execution of
multiple objects without creating multiple threads.  The game "EVE
Online" uses Stackless Python for this purpose. 
 
I doubt you could accomplish something similar in IronPython simply
because of how the CLR works.  But the low-level hooks in CLR 2.0 that
were created for SQL Server may allow something in that direction.
 
1: http://www.stackless.com
2: http://www.eve-online.com
 
--
Curt Hagenlocher
curt at hagenlocher.org

-- 
This message may contain confidential, proprietary, or legally privileged information. No confidentiality or privilege is waived by any transmission to an unintended recipient. If you are not an intended recipient, please notify the sender and delete this message immediately. Any views expressed in this message are those of the sender, not those of KBC Alternative Investment Management Limited or its affiliates, or any funds or accounts managed or advised by any of the aforementioned entities (together referred to as "KBC AIM"). 

This message does not create any obligation, contractual or otherwise, on the part of KBC AIM. It is not an offer (or solicitation of an offer) of, or a recommendation to buy or sell, any financial product. Any prices or other values included in this message are indicative only, and do not necessarily represent current market prices, prices at which KBC AIM would enter into a transaction, or prices at which similar transactions may be carried on KBC AIM's own books. The information contained in this message is provided "as is", without representations or warranties, express or implied, of any kind. Past performance is not indicative of future returns.



More information about the users mailing list