Fine thread control, Run only n bytecodes

David Pokorny davebrok at soda.csua.berkeley.edu
Tue Jul 20 23:00:37 EDT 2004


"Alan Kennedy" <alanmk at hotmail.com> wrote in message
news:40FA9D2A.3020905 at hotmail.com...
> David Pokorny wrote:
> > I'd like to be able to take a function or other chunk of code (that
someone
> > else has written), run it for, say 50 byte codes, and then return
control
> > back to my program/controlling thread until my program/controlling
thread
> > 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
fine
> > control?
>
> There was a similar thread to this last year which you may find useful.
>
> http://mail.python.org/pipermail/python-list/2003-July/173671.html
>
http://groups.google.com/groups?threadm=g7xRa.305%24fF.9914%40nnrp1.ozemail.com.au
>
> 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: http://xhaus.com/headers
> email alan:              http://xhaus.com/contact/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