| Store | Cart

Example Code - Named Pipes (Python 2.4 + ctypes on Windows)

From: Paul L. Du Bois <poly...@gmail.com>
25 Mar 2005 11:59:31 -0800
Srijit Kumar Bhadra wrote:
> Hello,> Here is an example of Multithreaded Pipe Server and Client using the> excellent ctypes library (Windows).

Coincidentally, the other day I just used named pipes in for the first
time.  I recommend using the excellent win32api extension; I believe it
is included by deafult in the ActiveState distro.

The API calls look fairly similar, but you pass strings instead of
c_whatever_p(), they return tuples, and they throw exceptions instead
of returning HRESULT h s.t. FAILED(h).  The resulting code feels much
more Pythonic.  For example, my first test looked like this:

    def testread(self):
        """Read all data currently in pipe."""
        while True:
                (nRead, nAvail, nMessage) =
win32pipe.PeekNamedPipe(self.hFile, 0)
                if nAvail:
                    (hr, data) = win32file.ReadFile(self.hFile, nAvail)
                    return data
            except pywintypes.error, e:
                errno = e.args[0]
                if errno == 109:  # other end disconnected

It's kind of cool that you can directly port C code to Python, but the
end result is IMO nigh-unreadable.


Recent Messages in this Thread
Srijit Kumar Bhadra Mar 25, 2005 01:39 pm
Paul L. Du Bois Mar 25, 2005 07:59 pm
Srijit Kumar Bhadra Mar 26, 2005 03:44 pm
Thomas Heller Mar 26, 2005 06:29 pm
Paul Du Bois Mar 26, 2005 07:21 pm
Harald Massa Mar 26, 2005 05:15 pm
Thomas Heller Mar 26, 2005 06:30 pm
Messages in this thread