Fine thread control, Run only n bytecodes

D

David Pokorny

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?

David Pokorny
 
C

Christopher T King

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?

Not that I know of (Python doesn't support per-bytecode hooks), but you
can get a similar effect after every X lines using settrace() and Lock
objects (untested):

count=0
main_lock=Lock()
thread_lock=Lock()

def local_tracer(f,e,a):
global main_lock, thread_lock, count
count-=1
if not count:
main_lock.release()
thread_lock.acquire()
return local_tracer

def global_tracer(f,e,a):
return local_tracer

def mythread():
global thread_lock
sys.settrace(tracer)
thread_lock.acquire()
do_stuff()

def waitfor(n):
global main_lock, thread_lock, count
count=n
thread_lock.release()
main_lock.acquire()

def main():
thread_lock.acquire()
main_lock.acquire()

start_thread(mythread)

while True:
waitfor(50)
do_stuff()

There's probably a more direct way about this, though.
 
A

Alan Kennedy

David said:
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/[email protected]

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,
 
D

David Pokorny

Alan Kennedy said:
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/[email protected]

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,

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
 
P

Paul Rubin

David Pokorny said:
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...

This subject has come up before. If you're trying to timeshare, I
don't think that's really going to help you. Some bytecodes can take
unbounded amounts of time to execute.
 
D

David Pokorny

Paul Rubin said:
This subject has come up before. If you're trying to timeshare, I
don't think that's really going to help you. Some bytecodes can take
unbounded amounts of time to execute.

As far as I know, the worst problem is with the likes of

BUILD_LIST
BUILD_TUPLE
UNPACK_SEQUENCE
....

For those who are interested, I've found the most promising solution so far:
a Python metacircular VM (written in Python).
http://www.codespeak.net/pipermail/pypy-dev/2003q1/000048.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top