[python-win32] (no subject)

Tim Roberts timr at probo.com
Thu Jan 22 20:34:05 CET 2009


dan at shragmir.com wrote:
> This is not the end of the world, because I can write the client
> application to use win32 API calls instead of the Python calls.  But does
> anyone have any notion of why this is happening?  I am hoping the answer
> may help me write clients in other applications (where I don’t have access
> to the win32 API).
>
> This code demonstrates the behavior:
>
> import win32file
> import win32pipe
> import threading
> import time
> class Server( threading.Thread ):
>     def run( self ):
>         self.pipeHandle = win32pipe.CreateNamedPipe(
>             '\\\\.\\pipe\\testpipe',
>             win32pipe.PIPE_ACCESS_DUPLEX,
>             win32pipe.PIPE_TYPE_BYTE |
>             win32pipe.PIPE_READMODE_BYTE |
>             win32pipe.PIPE_WAIT,
>             50,
>             4096,
>             4096,
>             10000,
>             None)
>
>         if self.pipeHandle == win32file.INVALID_HANDLE_VALUE:
>             print 'Failed to create named pipe!'
>             print 'Exiting...'
>             sys.exit(1)
>
>         win32pipe.ConnectNamedPipe( self.pipeHandle )
>         while True:
>             e, v = win32file.ReadFile( self.pipeHandle, 1, None )
>             if v == 'A':
>                 print 'SERVER: Received request "%s"--answering' %v
>                 err, j = win32file.WriteFile( self.pipeHandle, 'B' )
>                 win32file.FlushFileBuffers( self.pipeHandle )
>             else:
>                 print 'SERVER: Received request "%s"--exiting' %v
>                 break
>
>         print "SERVER: Exiting server"
>
> SERVER = Server()
> SERVER.start()
> time.sleep(0.1)
> CLIENT_PIPE = open( '\\\\.\\pipe\\testpipe', 'a+b' )
>   

You probably want "r+b", although I don't think it really makes a
difference here.

> for i in range( 10 ):
>     CLIENT_PIPE.write( 'A' )
>     CLIENT_PIPE.flush()
>     reply = CLIENT_PIPE.read( 1 )
>     print 'CLIENT: answer %d received: "%s"'%(i, reply)
>   

The issue here is with the Python file wrappers.  You have to take SOME
kind of action to allow the Python file wrapper to turn around from
reading back to writing.  If you add
    CLIENT_PIPE.seek(0,0)
at the end of the loop, after the read, then it works.  That is...

> CLIENT_PIPE.WRITE( 'C' )
> CLIENT_PIPE.flush()
>   

...it works after you fix the spelling of "WRITE" there.  ;)

-- 
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.



More information about the python-win32 mailing list