[IPython-dev] variable information request
Jason Grout
jason-sage at creativetrax.com
Sat Apr 9 07:53:18 EDT 2011
Here's a crazy idea that could lead to some nice interactively-updating
components of an IPython frontend. Right now, in the new messaging
protocol, it appears that I can send a computation request and then ask
for the output to contain the values of several variables (the
"user_variables" field of a execute_reply message). However, what if I
want to check the value of a variable *during* a computation and get an
immediate response? I might, for example, have a box that prints out
the current value of a root approximation, for example, or a slider that
contains the current iteration number, and I want these to be updated
fairly frequently while the computation is running.
Here's one way to do it, I think: Run a separate thread that just
answers these queries. Since the GIL handles access to variables, I
think it's okay for the separate thread to retrieve the value of a
variable and return that, and it seamlessly does this while the main
thread is carrying on a computation. For example, the following code
will print out the current iteration number every time enter is pressed:
--------------------------------------
from threading import Timer, Thread
import time
def printi():
global i
while True:
raw_input('Press enter to see the iteration number')
print i
t=Thread(target=printi)
t.daemon=True
t.start()
for i in range(20):
time.sleep(.5)
---------------------------------------
I can see a problem if the value you are querying actually changes the
object state in the middle of another computation using that object. But
simple queries about an object's value should work fine, and maybe we
could leave it up to the user to not mess up a currently-running
computation by changing an object state.
What do people think?
Thanks,
Jason
--
Jason Grout
More information about the IPython-dev
mailing list