Fine thread control, Run only n bytecodes

David Pokorny davebrok at
Wed Jul 21 05:00:37 CEST 2004

"Alan Kennedy" <alanmk at> wrote in message
news:40FA9D2A.3020905 at
> David Pokorny wrote:
> > I'd like to be able to take a function or other chunk of code (that
> > else has written), run it for, say 50 byte codes, and then return
> > back to my program/controlling thread until my program/controlling
> > indicates that it wants to run the next 50 bytes codes of the foreign
> > program, etc...
> >
> > Is there an extension to the python threading module that supports such
> > control?
> There was a similar thread to this last year which you may find useful.
> Multiple possible approaches to the problem were discussed.
> Particularly impressive was Duncan Booth's tour-de-force manipulation
> of the target program at a byte-code level.
> regards,
> -- 
> alan kennedy
> ------------------------------------------------------
> check http headers here:
> email alan:    

Thanks for the link. Duncan's code is so close it hurts! The generator-yield
paradigm seems to be the right solution when steplocking _statements_ is the
required behavior. I'm interested in steplocking chunks of bytecodes: we
have two functions that are trying to solve a problem simultaneously (such
as getting out of a maze or playing tag). Lock-stepping statements would
reward programs that cram as much as they can into a single statement.

def foo(list):
    return (math.pow( (1+math.sqrt(5)/2), max( [x*x - x for x in list if x >
3]))/math.sqrt(5) + math.pow( (1-math.sqrt(5)/2), max( [x*x - x for x in
list if x > 3]))/math.sqrt(5))

Naturally, I'd like to discourage this sort of thing in client programs.

I'd consider mucking around with the actual bytecode, but there are
difficulties: inserting bytecode without discretion will mess up relative
jumps, even if one could save all the registers _and_ the stack.

Given all this, it looks like the best option is to hack Python/ceval.c

David Pokorny

More information about the Python-list mailing list