Show
Ignore:
Timestamp:
11/14/03 21:13:11 (20 years ago)
Author:
jalet
Message:

We exit the loop too soon.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • pykota/trunk/bin/cupspykota

    r1183 r1184  
    2424# 
    2525# $Log$ 
     26# Revision 1.6  2003/11/14 20:13:11  jalet 
     27# We exit the loop too soon. 
     28# 
    2629# Revision 1.5  2003/11/14 18:31:27  jalet 
    2730# Not perfect, but seems to work with the poll() loop. 
     
    146149def sigterm_handler(signum, frame) : 
    147150    """Handler for SIGTERM.""" 
     151    sys.stderr.write("INFO: PyKota backend aborted.") 
    148152    sys.exit(1) 
    149153     
     
    235239        subprocess = PyKotaPopen3([realbackend] + sys.argv[1:], capturestderr=1, arg0=os.environ["DEVICE_URI"]) 
    236240        pollster = select.poll() 
    237         pollster.register(infile.fileno(), select.POLLIN) 
    238         pollster.register(subprocess.fromchild.fileno(), select.POLLIN) 
    239         pollster.register(subprocess.childerr.fileno(), select.POLLIN) 
    240         pollster.register(subprocess.tochild.fileno(), select.POLLOUT) 
    241         inputdata = "" 
     241        infno = infile.fileno() 
     242        stdoutfno = sys.stdout.fileno() 
     243        stderrfno = sys.stderr.fileno() 
     244        fromcfno = subprocess.fromchild.fileno() 
     245        tocfno = subprocess.tochild.fileno() 
     246        cerrfno = subprocess.childerr.fileno() 
     247        pollster.register(infno, select.POLLIN | select.POLLPRI) 
     248        pollster.register(fromcfno, select.POLLIN | select.POLLPRI) 
     249        pollster.register(cerrfno, select.POLLIN | select.POLLPRI) 
     250        pollster.register(stdoutfno, select.POLLOUT) 
     251        pollster.register(stderrfno, select.POLLOUT) 
     252        pollster.register(tocfno, select.POLLOUT) 
     253        indata = "" 
     254        outdata = "" 
     255        errdata = "" 
    242256        end = 0 
    243257        while not end : 
    244             availablestreams = pollster.poll() 
    245             for (stream, mask) in availablestreams : 
    246                 if mask & select.POLLHUP : 
    247                     if stream == infile.fileno() : 
    248                         end = 1 
    249                 if mask & select.POLLOUT : 
    250                     if stream == subprocess.tochild.fileno() : 
    251                         if inputdata : 
    252                             os.write(stream, inputdata)     
    253                             inputdata = "" 
    254                 if mask & select.POLLIN :      
    255                     data = os.read(stream, 256 * 1024) 
    256                     if stream == infile.fileno() : 
    257                         inputdata += data 
    258                     elif stream == subprocess.fromchild.fileno() : 
    259                         sys.stdout.write(data) 
    260                     elif stream == subprocess.childerr.fileno() :     
    261                         sys.stderr.write(data) 
    262         if inputdata :                 
     258            availablefds = pollster.poll(500) 
     259            if not availablefds : 
     260                end = 1 
     261            else :     
     262                for (fd, mask) in availablefds : 
     263                    thebackend.logdebug("Stream: %02i  Mask: %02x" % (fd, mask)) 
     264                    if mask & select.POLLHUP : 
     265                        if fd == infno : 
     266                            end = 1 # this happens with real stdin 
     267                    if mask & select.POLLOUT : 
     268                        if fd == tocfno : 
     269                            if indata : 
     270                                os.write(fd, indata)     
     271                                indata = "" 
     272                        elif fd == stdoutfno : 
     273                            if outdata : 
     274                                os.write(fd, outdata) 
     275                                outdata = "" 
     276                        elif fd == stderrfno : 
     277                            if errdata : 
     278                                os.write(fd, errdata) 
     279                                errdata = "" 
     280                    if (mask & select.POLLIN) or (mask & select.POLLPRI) :      
     281                        data = os.read(fd, 256 * 1024) 
     282                        if fd == infno : 
     283                            indata += data 
     284                            if not data : 
     285                                end = 1 # this happens with real files 
     286                        elif fd == fromcfno : 
     287                            outdata += data 
     288                        elif fd == cerrfno :     
     289                            errdata += data 
     290        if indata :                 
    263291            try : 
    264                 os.write(subprocess.tochild.fileno(), inputdata) 
     292                os.write(tocfno, indata) 
    265293            except :     
    266294                pass