Maybe I have reinvented the wheel, but I find the attached code snippet
very useful.
I have modified a bit the recipe at
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/65222
to be used in the following situation:
You plot (interactively) something in a main program, which than works
heavily. Normally, when the figure window gets obscured, it will not
redraw. The attached code shows how to make a thread that periodically
redraws the figure.
r.
import threading
import atexit
##
# Extended from http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/65222.
# 22.03.2007, c
class TaskThread( threading.Thread ):
"""Thread that executes a given function every N seconds"""
def __init__( self, fun, args = (), interval = 0.2 ):
"""
Create the task thread with:
fun .. function to run
args .. tuple of function arguments
interval .. number of seconds we sleep between executing the function
"""
threading.Thread.__init__( self )
self.finished = threading.Event()
self.interval = interval
self.fun = fun
self.args = args
self.canRun = False
# Respond to Ctrl_C.
atexit.register( self.shutdown )
def setInterval( self, interval ):
"""Set the number of seconds we sleep between executing the function."""
self.interval = interval
def shutdown( self ):
"""Stop this thread"""
self.finished.set()
def run(self):
while 1:
if self.finished.isSet(): return
if self.canRun:
self.task()
# sleep for interval or until shutdown
self.finished.wait( self.interval )
def task(self):
apply( self.fun, self.args )
def on( self ):
self.canRun = True
def off( self ):
self.canRun = False
if __name__ == '__main__':
import pylab
# Make a figure interactively, so that a window appears.
pylab.ion()
fig = pylab.figure()
pylab.ioff()
# Start the redrawing thread. The redrawing is switched off.
thread = TaskThread( fig.canvas.draw )
thread.start()
# Switch off the redrawing (no-operation here...)
thread.off()
# Make some plots.
ax = fig.add_subplot( 111 )
ax.plot( range( 10 ) )
# Switch on the redrawing.
thread.on()
# Do some work.
for ii in xrange( 10000000 ):
if not( ii % 1e5 ):
print 'heavy work!', ii
# Finish the thread.
thread.shutdown()
thread.join()
# Show() the figure.
pylab.show()
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users