Windows freeze up Python/Motif

Sjoerd Mullender sjoerd at oratrix.nl
Fri Feb 25 12:02:12 EST 2000


I assume you start the program with os.popen and read from a pipe.
Something like

f = os.popen('long-running-perl-program', 'r')
while 1:
    line = f.readline()
    if not line: break
    updatedialog(line)

I'll also assume that you use the python X extension.  If not, the
basics of what I say still apply.

You'll have to redo this using callbacks.  Something like:

f = os.popen('long-running-perl-program', 'r')
Xt.AddInput(f.fileno(), Xtdefs.XtInputReadMask, read_callback, f)
# now make sure that you return to the X mainloop, since that's what
# will cause the callback to be called

def read_callback(f, fd, id):
    # f is the last argument to Xt.AddInput, fd and id are provided by
    # the X library and are the file descriptor used and the ID which
    # was returned by Xt.AddInput
    fcntl(fd, FCNTL.F_SETFL, FCNTL.O_NDELAY) # put in no-delay mode
    while 1:
        data = os.read(fd, 128)
	if not data: break
        updatedialog(data) # not necessarily a whole line
    fcntl(fd, FCNTL.F_SETFL, 0) # back to normal mode

What this does is tell the Xt library to look at the given file
(f.fileno()) to see if there is data to read, and if there is to call
the callback function.  The callback function reads the data and
updates the dialog.  The callback function should make sure not to
hang in the call to read.  If any other X event come along (such as
expose events), the normal expose callback gets called behind the
scenes.

The calls to fcntl don't have to be in the callback.  The call to set
the file descriptor in no-delay mode should be done before returning
to the X mainloop.

I hope this helps.

On Fri, Feb 25 2000 bragib at my-deja.com wrote:

> I have a dialog shell set up that when a certain button is pressed
> it fires off a perl script which is really lengthy in execution.
> I am reading each line from stdout and printing it to a message
> are in my dialog.  So the dialog only gets updated when I get a new
> line.  Well the time between new lines can take awhile and
> in the meantime the gui freezes up (i.e. if I drag another
> window over the dialog does not get updated).
> 
> I do not have the option of splitting up the execution of this
> perl script.  Does anyone know how I can submit the process
> in 'parallel' and have the dialog wait for the process to
> complete?
> 
> I am using Motif through a Python binding if that helps?
> 
> Thanks in advance
> 
> 
> Sent via Deja.com http://www.deja.com/
> Before you buy.

-- Sjoerd Mullender <sjoerd.mullender at oratrix.com>




More information about the Python-list mailing list