Autonomous display of image/plot/figure

I'm not sure if this is the right venue for this question. I've searched the
archives, but without success so far. If this is covered there (or elsewhere
on the web), I'd apprciate a pointer to it so it doesn't duplicate bandwidth
here.

Anyway, what I'd like to do is have a python script which reads data from a
file, displays an image/plot/whatever made from the data, and then exits,
keeping the image displayed.

I'm running Fedora 8, python 2.5.1, and matplotlib 0.91.2-1.fc8 from the yum
repository. Backend is set to GTKAgg in my matplotlibrc file.

My initial attempt used the "double fork" method from the python cookbook:

- -------------Code follows----------------------
  if __name__ == "__main__":

  #From Python Cookbook
  try:
    pid = os.fork()
    if pid > 0:
      # Exit first parent
      sys.exit(0)
  except OSError, e:
    print >>sys.stderr, "fork #1 failed: %d (%s)" %(e.errno, e.strerror)
    sys.exit(1)

  # Decouple from parent environment
  #os.chdir("/")
  os.setsid()
  os.umask(0)

  # Do second fork
  try:
    pid = os.fork()
    if pid > 0:
      # Exit from second parent; print eventual PID before exiting
      print "Image PID %d" % pid
      sys.exit(0)
  except OSError, e:
    print >>sys.stderr, "fork #2 failed: %d (%s)"%(e.errno, e.strerror)
    sys.exit(1)

  # Start the main loop to display image
  main()
  
- --------------END CODE--------------------------------------

The main() function reads the values appropriately into the variable "myarr",
and then calls imshow and show:

- ------------ Code follows -------------------
       :
       :
  pylab.imshow(myarr)
  pylab.show()
- --------------END CODE--------------------------------------

. . . and then exits.

All works well until I try to kill the figure/image by clicking on the X in
the upper-right corner. It disappears alright, but the process remains
running, and I have to manually kill it with the kill -SIGTERM <pid> command.

I'd like the process to die when I close the window.

I'm really an application programmer, not a system programmer, and usually
don't delve this deeply into process management, so I'm probably doing
something extremely ignorant. Help is appreciated.

Thanks!

  James

Why do you want to "fork" the process? If you just run it in the background it should have the desired effect:

<begin tst.py>
from pylab import *
x = linspace(-10,10,100) # or load data from a file.
y = sin(x)
plot(x,y)
show()
<end tst.py>

$ python tst.py&

Process remains in background running until the user closes the plot window, at which point it terminates.

Michael.

···

On 7 Jul 2008, at 2:30 PM, James K. Gruetzner wrote:

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I'm not sure if this is the right venue for this question. I've searched the
archives, but without success so far. If this is covered there (or elsewhere
on the web), I'd apprciate a pointer to it so it doesn't duplicate bandwidth
here.

Anyway, what I'd like to do is have a python script which reads data from a
file, displays an image/plot/whatever made from the data, and then exits,
keeping the image displayed.

I'm running Fedora 8, python 2.5.1, and matplotlib 0.91.2-1.fc8 from the yum
repository. Backend is set to GTKAgg in my matplotlibrc file.

My initial attempt used the "double fork" method from the python cookbook:

- -------------Code follows----------------------
  if __name__ == "__main__":

  #From Python Cookbook
  try:
    pid = os.fork()
    if pid > 0:
      # Exit first parent
      sys.exit(0)
  except OSError, e:
    print >>sys.stderr, "fork #1 failed: %d (%s)" %(e.errno, e.strerror)
    sys.exit(1)

  # Decouple from parent environment
  #os.chdir("/")
  os.setsid()
  os.umask(0)

  # Do second fork
  try:
    pid = os.fork()
    if pid > 0:
      # Exit from second parent; print eventual PID before exiting
      print "Image PID %d" % pid
      sys.exit(0)
  except OSError, e:
    print >>sys.stderr, "fork #2 failed: %d (%s)"%(e.errno, e.strerror)
    sys.exit(1)

  # Start the main loop to display image
  main()

- --------------END CODE--------------------------------------

The main() function reads the values appropriately into the variable "myarr",
and then calls imshow and show:

- ------------ Code follows -------------------
       :
  pylab.imshow(myarr)
  pylab.show()
- --------------END CODE--------------------------------------

. . . and then exits.

All works well until I try to kill the figure/image by clicking on the X in
the upper-right corner. It disappears alright, but the process remains
running, and I have to manually kill it with the kill -SIGTERM <pid> command.

I'd like the process to die when I close the window.

I'm really an application programmer, not a system programmer, and usually
don't delve this deeply into process management, so I'm probably doing
something extremely ignorant. Help is appreciated.

Thanks!

  James

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)

iD8DBQFIcor8xOXthSHeGJIRAm/aAKC/SPQzocHigz2glpvtBJc0BcMU3ACfUTe0
PM0fby8/z3YJcAj+Llb++ho=
=NgA/
-----END PGP SIGNATURE-----

-------------------------------------------------------------------------
Sponsored by: SourceForge.net Community Choice Awards: VOTE NOW!
Studies have shown that voting for your favorite open source project,
along with a healthy diet, reduces your potential for chronic lameness
and boredom. Vote Now at http://www.sourceforge.net/community/cca08
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
matplotlib-users List Signup and Options