473,395 Members | 2,467 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Fine thread control, Run only n bytecodes

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
Jul 18 '05 #1
5 1391
On Fri, 16 Jul 2004, 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?


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.

Jul 18 '05 #2
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/pyt...ly/173671.html
http://groups.google.com/groups?thre...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
Jul 18 '05 #3

"Alan Kennedy" <al****@hotmail.com> wrote in message
news:40**************@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/pyt...ly/173671.html

http://groups.google.com/groups?thre...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
Jul 18 '05 #4
"David Pokorny" <da******@soda.csua.berkeley.edu> writes:
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.
Jul 18 '05 #5

"Paul Rubin" <http://ph****@NOSPAM.invalid> wrote in message
news:7x************@ruckus.brouhaha.com...
"David Pokorny" <da******@soda.csua.berkeley.edu> writes:
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.


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/p...q1/000048.html
Jul 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: adeger | last post by:
Having trouble with my first forays into threads. Basically, the threads don't seem to be working in parallel (or you might say are blocking). I've boiled my problems to the following short code...
3
by: Maurice LING | last post by:
I know this might sounds wierd but I'm wondering if I can use pexpect or os.popen3 function to invoke and control python interpreter to make it act like a python interpreter in python? maurice
9
by: Harald Armin Massa | last post by:
I need to do some synchronisations like in a cron.job import time from threading import Thread class updater(Thread): def run(self): while True: do_updates() time.sleep(600)
7
by: Charles Law | last post by:
My first thought was to call WorkerThread.Suspend but the help cautions against this (for good reason) because the caller has no control over where the thread actually stops, and it might have...
5
by: Mark R. Dawson | last post by:
Hi all, I may be missing something with how databinding works but I have bound a datasource to a control and everything is great, the control updates to reflect the state of my datasource when I...
8
by: =?Utf-8?B?R3JlZyBMYXJzZW4=?= | last post by:
I'm trying to figure out how to modify a panel (panel1) from a backgroundworker thread. But can't get the panel to show the new controls added by the backgroundwork task. Here is my code. In...
10
by: Paul E Collins | last post by:
I want to fill an ImageList with bitmaps for a ListView from another thread, because it's a time-consuming process. I expect the ListViewItems' images to "load" one by one, as in a Web browser. ...
10
by: sophie_newbie | last post by:
Hi, I'm trying to write a piece of code that spawns a thread and prints dots every half second until the thread spawned is finished. Code is something like this: import threading class...
3
by: kimiraikkonen | last post by:
Hi, I was looking for an example on CodeProject and saw an interesting thing, i downloaded the article source code and converted to my VB 2005 Express and compiled with no problem, however when i...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.