Sharing Pipes in win32

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nir of the Waves

    Sharing Pipes in win32

    In windows platforms how can I make two processes hold two ends of the same pipe?



    os.pipe() returns reader and writer descriptors but how do I export one of the ends to another process?



    Note: popen() is not good for me since the new process is left without tty input and output.






  • Mark Hammond

    #2
    Re: Sharing Pipes in win32

    Nir of the Waves wrote:
    [color=blue]
    > In windows platforms how can I make two processes hold two ends of the
    > same pipe?
    >
    >
    >
    > os.pipe() returns reader and writer descriptors but how do I export one
    > of the ends to another process?
    >
    >
    >
    > Note: popen() is not good for me since the new process is left without
    > tty input and output.[/color]

    I believe you can either do it via a child process and handle
    inheritance (meaning os.* functions should work too), or via named pipes
    (meaning you will need to use win32process).

    Mark.

    Comment

    • Brian Kelley

      #3
      Re: Sharing Pipes in win32

      Nir of the Waves wrote:[color=blue]
      > In windows platforms how can I make two processes hold two ends of the
      > same pipe?
      >
      >
      >
      > os.pipe() returns reader and writer descriptors but how do I export one
      > of the ends to another process?
      >
      >
      >
      > Note: popen() is not good for me since the new process is left without
      > tty input and output.
      >[/color]

      I have a little namedpipe class that I use. I stole it somewhat from a
      perl version a while ago.

      """Named pipe wrapper.

      #server
      from NamedPipe import NamedPipe
      pipe = NamedPipe("mypi pe")
      pipe.connect()
      pipe.write("foo ")

      #client
      from NamedPipe import NamedPipe, CLIENT
      import time
      pipe = NamedPipe("\\\\ .\\pipe\\test1" , mode=CLIENT)
      while pipe.select() is None:
      time.sleep(1)
      print pipe.read()

      """
      from win32pipe import *
      from win32file import *
      import pywintypes
      import win32security, win32file
      import string

      SERVER = 0
      CLIENT = 1
      class NamedPipe:
      _header = '\\\\.\\pipe\\'
      def __init__(self,
      pipeName,
      mode=SERVER,
      openMode=PIPE_A CCESS_DUPLEX,
      pipeMode=0,
      maxInstances=1,
      outBufferSize=0 ,
      inBufferSize=0,
      defaultTimeOut= 0,
      securityAttribu tes=None):
      # fix the name if appropriate
      pipeName = self._fixName(p ipeName)

      sAttrs = win32security.S ECURITY_ATTRIBU TES()
      sAttrs.bInherit Handle = 1

      self._pipeName = pipeName
      # allocate a 100K readbuffer
      self._bufsize = 100000
      self._buf = win32file.Alloc ateReadBuffer(s elf._bufsize)

      if mode:
      WaitNamedPipe(p ipeName, NMPWAIT_WAIT_FO REVER)
      self._pipe = CreateFile(pipe Name,
      GENERIC_READ | GENERIC_WRITE,
      FILE_SHARE_READ | FILE_SHARE_WRIT E,
      None,
      OPEN_EXISTING,
      FILE_ATTRIBUTE_ NORMAL | FILE_FLAG_WRITE _THROUGH,
      None)

      else:
      self._pipe = CreateNamedPipe (pipeName, openMode, pipeMode,
      maxInstances,
      outBufferSize, inBufferSize,
      defaultTimeOut, securityAttribu tes)

      def __del__(self):
      if self._pipe:
      try:
      self.disconnect ()
      except pywintypes.erro r:
      # I don't know why this happens...
      pass

      def _fixName(self, pipeName):
      """(pipeNam e)->fixed pipeName
      Pipe names must start with header '\\.\pipe\',
      if the given pipeName
      does not, append the header to the pipeName"""
      if string.find(pip eName, self._header) != 0:
      return "%s%s"%(self._h eader, pipeName)
      else:
      return pipeName

      def connect(self):
      return ConnectNamedPip e(self._pipe, None)

      def disconnect(self ):
      result = DisconnectNamed Pipe(self._pipe )
      self._pipe = None
      return result

      def select(self):
      """returns None is the pipe has no data, self otherwise"""
      data, size, something = PeekNamedPipe(s elf._pipe, 1)
      if not data:
      return None
      return data

      def write(self, data):
      win32file.Write File(self._pipe , data, None)

      def read(self):
      length, s = win32file.ReadF ile(self._pipe, self._buf)
      return s

      if __name__ == "__main__":
      import tempfile
      import thread, time
      done = 0
      def runpipe():
      pipe = NamedPipe("test 1")
      pipe.connect()
      pipe.write('Sen ding to pipe')
      def readpipe():
      global done
      pipe2 = NamedPipe("test 1", mode=CLIENT)
      print pipe2.read()
      done = 1

      thread.start_ne w_thread(runpip e, ())
      thread.start_ne w_thread(readpi pe, ())
      while not done:
      time.sleep(1)




      Comment

      Working...