[Edu-sig] Strange transitive import problem

John Zelle john.zelle at wartburg.edu
Mon Feb 12 20:23:27 CET 2007


Hi Peter,

You have identified the crux of the matter. Basically what happens is that the 
import apparently locks the interpreter so that the graphics thread does not 
get a chance to run (import is atomic?). So any call into the graphics 
library (like creating a GraphWin) deadlocks, since the call cannot return 
until the thread runs, and the thread won't run until the import is complete.

I could "fix" this by having my graphics package delay the thread launch until 
the first graphics call is made. But that means _every_ graphics operation 
will have to check to see if the thread has been launched yet. Even if that's 
not a big efficiency issue (I'm not sure), it feels too ad hoc to me, so I've 
resisted doing it. I may resort to that in the next release.

By the way, I personally would pair the initGraphics() with a closeGraphics() 
operation that closes the window clean up at the end.

--John



On Monday 12 February 2007 10:59 am, Peter Drake wrote:
> I tried just tacking all of my procedural wrappers on to the end of
> your file so there would be no transitive import. Surprisingly, that
> didn't work. The trick you mention below DOES work, and I'll use that
> as a workaround.
>
> A clue, then: it's not the transitive importing that causes the
> problem, it's the importing of a file that calls GraphWin().
>
> Peter Drake
> Assistant Professor of Computer Science
> Lewis & Clark College
> http://www.lclark.edu/~drake/
>
> On Feb 9, 2007, at 1:09 PM, John Zelle wrote:
> > Peter,
> >
> > I should have caught this when you posted your file. There is a
> > bizarre import
> > interaction that I am aware of, but can't say that I fully understand
> > regarding my graphics library. It seems that Python imports are in
> > some-way,
> > somewhat atomic, and with the way my graphics library is set up
> > this can
> > produce this deadlock. I have a workaround for you. Move the
> > graphics setup
> > code in your module into a function:
> >
> > def initGraphics():
> >     global root
> >     root = z.GraphWin()
> >
> >     print "If you can't see the graphics window,"
> >     print "it's probably at the upper left"
> >     print "of your screen, behind this one."
> >
> > Then call this function once at the start of your graphics
> > programs. This is
> > what will now pop up the window. So your "junk" program becomes:
> >
> > from graphics2 import *
> > print "imported graphics2"
> > initGraphics()
> > background("red")
> >
> > If someone has a better understanding of Python module imports and
> > why that
> > causes a problem with my graphics package, I'd love to know how to
> > fix this
> > problem rather than working around it.
> >
> > --John
> >
> > On Friday 09 February 2007 1:36 pm, Peter Drake wrote:
> >> The graphics2.py file I supplied earlier (http://www.lclark.edu/
> >> ~drake/courses/cs0/graphics2.py) is a procedural front-end for
> >> Zelle's graphics.py.
> >>
> >> If I run the graphics2.py module, everything works fine, and I can
> >> issue commands interactively.
> >>
> >> A problem happens if I write ANOTHER program, say junk.py:
> >>
> >> from graphics2 import *
> >> print "imported graphics2"
> >> background("red")
> >>
> >> When I run this (within IDLE), it hangs trying to import graphics2.
> >> There is no error message or processor load, it just hangs.
> >>
> >> Can anyone explain (or reproduce) this behavior?
> >>
> >> Peter Drake
> >> Assistant Professor of Computer Science
> >> Lewis & Clark College
> >> http://www.lclark.edu/~drake/
> >>
> >>
> >>
> >> _______________________________________________
> >> Edu-sig mailing list
> >> Edu-sig at python.org
> >> http://mail.python.org/mailman/listinfo/edu-sig
> >
> > --
> > John M. Zelle, Ph.D.             Wartburg College
> > Professor of Computer Science    Waverly, IA
> > john.zelle at wartburg.edu          (319) 352-8360
> > _______________________________________________
> > Edu-sig mailing list
> > Edu-sig at python.org
> > http://mail.python.org/mailman/listinfo/edu-sig
>
> _______________________________________________
> Edu-sig mailing list
> Edu-sig at python.org
> http://mail.python.org/mailman/listinfo/edu-sig

-- 
John M. Zelle, Ph.D.             Wartburg College
Professor of Computer Science    Waverly, IA     
john.zelle at wartburg.edu          (319) 352-8360  


More information about the Edu-sig mailing list