title: Using and Overlapped Named Pipe in Python 3.7
body: <p>I am using Windows with Python 3.7 and I am trying to asynchronly share data, ony strings, between to python processes. One of the is running indefinitely (reciever), the other one may start at any point sends some data and then ends (sender). I am trying to use a named pipe for that.</p>
  
  <p>I managed to get this when they run synchronously (the reciever waiting at an blocked pipe until he gets data), however the reciever has other stuff to do so ideally he shouldn't wait all the time. Also there might be a second sender at some point so a blocked pipe isin't great.</p>
  
  <p>The code for the receiver is:</p>
  
  <pre><code>import os
  import time
  import sys
  import win32pipe, win32file, pywintypes
  
  pipe_name = r'\\.\pipe\mypipe' 
  
  pipe = win32pipe.CreateNamedPipe(
          pipe_name,
          win32pipe.PIPE_ACCESS_DUPLEX | win32file.FILE_FLAG_OVERLAPPED,  # open mode 
          win32pipe.PIPE_TYPE_MESSAGE | win32pipe.PIPE_READMODE_MESSAGE | win32pipe.PIPE_WAIT, # pipe mode
          1, 65536, 65536, # max instances, out buffer size,  in buffer size
          0, # timeout
      None)
  
  
  while 1:
      print("doing my other stuff")
      try:
          win32pipe.ConnectNamedPipe(pipe, pywintypes.OVERLAPPED())
      except pywintypes.error as e:
          if e.winerror == 232:   #disconnected pipe
              win32pipe.DisconnectNamedPipe(pipe)
              print("Disconnecting pipe")
          else:
              print(e)
      try:
          retval, msg = win32file.ReadFile(pipe, 0, pywintypes.OVERLAPPED())
          print(msg)
      except pywintypes.error as e:
          if e.winerror == 536: #Wating for connection
              print("waiting")
          elif e.winerror == 233: #no process on other side
              continue
      time.sleep(1)
  </code></pre>
  
  <p>The code for the sender is:</p>
  
  <pre><code>import os
  import time
  import sys
  import win32pipe, win32file, pywintypes
  
  
  pipe_name = r'\\.\pipe\mypipe' 
  
  
  for x in range(5):
  
      handle = win32file.CreateFile(
              pipe_name,
              win32file.GENERIC_READ | win32file.GENERIC_WRITE,
              0,
              None,
              win32file.OPEN_EXISTING,
              win32file.FILE_FLAG_OVERLAPPED,
              None
              )
      res = win32pipe.SetNamedPipeHandleState(handle, win32pipe.PIPE_READMODE_MESSAGE, None, None)
      print(f"sending {x}")
      win32file.WriteFile(handle, str.encode(f"hello world {x}"))
      win32file.CloseHandle(handle)
      time.sleep(2)
  </code></pre>
  
  <p>Right now both can run and have some connection but i cant really get the data. The reciever can do oter stuff and disconnects and reopens the pipe if something is send, but <code>msg</code> ends up being empty. If i stop it in the debugger and send something the value of <code>msg</code> gets "memory at 0x0......." which I would interpret as some sort of pointer, yet as you probalby already noticed my understaing of pipes is limited.</p>
  
  <p><a href="https://stackoverflow.com/a/51239081/5124383">Here</a> I found a good example for a synchronos pipe which worked. I changed the the creation of the pipe to the reciever but that wasn't to hard. I found some examples for asynchronous (overlapped) pipes <a href="https://www.programcreek.com/python/example/59664/pywintypes.OVERLAPPED" rel="nofollow noreferrer">here</a>, which were also great but left me with the issue i am facing now.</p>
  
  <p>Ist reading from an overlapped pipe still a task for <code>win32file.ReadFile</code>or is their something else I am missing?</p>
  
  <p>Thank you very much!</p>