Python Forum
All pipe instances are busy
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
All pipe instances are busy
#1
Hi
I am trying to use pipes to communicate between 2 python apps
On the server side I create a named pipe
      
self.server_pipe = win32pipe.CreateNamedPipe(r'\\.\pipe\DataUpdate',
                                                     win32pipe.PIPE_ACCESS_DUPLEX,
                                                     win32pipe.PIPE_TYPE_MESSAGE | win32pipe.PIPE_READMODE_MESSAGE | win32pipe.PIPE_WAIT,
                                                     win32pipe.PIPE_UNLIMITED_INSTANCES,
                                                     65536,
                                                     65536,
                                                     0,
                                                     None)
        win32pipe.ConnectNamedPipe(self.server_pipe, None)
Then on the client side
        self.client_pipe = win32file.CreateFile(r'\\.\pipe\DataUpdate',
                                                win32file.GENERIC_READ | win32file.GENERIC_WRITE,
                                                0,
                                                None,
                                                win32file.OPEN_EXISTING,
                                                win32file.FILE_ATTRIBUTE_NORMAL,
                                                None)
        res = win32pipe.SetNamedPipeHandleState(self.client_pipe,
                                                win32pipe.PIPE_READMODE_MESSAGE,
                                                None,
                                                None)
I start the server first but when I run the client I get
pywintypes.error: (231, 'CreateFile', 'All pipe instances are busy.')

Have I missed something?
Reply
#2
You should disconnect the pipe from server first.

demo server code:
import win32file
import pywintypes
import win32pipe
import win32event
import winerror
# https://resources.oreilly.com/examples/9781565926219/-/tree/master/ch18_services

pipeName = r"\\.\pipe\PipeDebug"


openMode = win32pipe.PIPE_ACCESS_DUPLEX | win32file.FILE_FLAG_OVERLAPPED
pipeMode = win32pipe.PIPE_TYPE_MESSAGE

# When running as a service, we must use special security for the pipe
sa = pywintypes.SECURITY_ATTRIBUTES()
# Say we do have a DACL, and it is empty
# (ie, allow full access!)
sa.SetSecurityDescriptorDacl(1, None, 0)

pipeHandle = win32pipe.CreateNamedPipe(
    pipeName,
    openMode,
    pipeMode,
    win32pipe.PIPE_UNLIMITED_INSTANCES,
    0,
    0,
    6000,  # default buffers, and 6 second timeout.
    sa,
)

hWaitStop = win32event.CreateEvent(None, 0, 0, None)
# We need to use overlapped IO for this, so we dont block when
# waiting for a client to connect.  This is the only effective way
# to handle either a client connection, or a service stop request.
overlapped = pywintypes.OVERLAPPED()
# And create an event to be used in the OVERLAPPED object.
overlapped.hEvent = win32event.CreateEvent(None, 0, 0, None)

# Loop accepting and processing connections
while 1:
    try:
        hr = win32pipe.ConnectNamedPipe(pipeHandle, overlapped)
    except Exception as e:
        print("Error connecting pipe!", e)
        pipeHandle.Close()
        break

    if hr == winerror.ERROR_PIPE_CONNECTED:
        # Client is fast, and already connected - signal event
        win32event.SetEvent(overlapped.hEvent)
    # Wait for either a connection, or a service stop request.
    timeout = win32event.INFINITE
    waitHandles = hWaitStop, overlapped.hEvent
    rc = win32event.WaitForMultipleObjects(waitHandles, 0, timeout)
    if rc == win32event.WAIT_OBJECT_0:
        # Stop event
        break
    else:
        # Pipe event - read the data, and write it back.
        # (We only handle a max of 255 characters for this sample)
        try:
            hr, data = win32file.ReadFile(pipeHandle, 256)
            win32file.WriteFile(pipeHandle, ("You sent me:" + data.decode()).encode())
            # And disconnect from the client.
            win32pipe.DisconnectNamedPipe(pipeHandle)
        except win32file.error:
            # Client disconnected without sending data
            # or before reading the response.
            # Thats OK - just get the next connection
            continue
Demo client code:
import win32pipe

if __name__ == "__main__":
    message = "aaa"
    pipeName = r"\\.\pipe\PipeDebug"
    data = win32pipe.CallNamedPipe(pipeName, message.encode(), 512, 0)
    print("The service sent back:")
    print(data)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  BrokenPipeError: [Errno 32] Broken pipe Pflaumboy 3 41,994 Jul-29-2019, 07:48 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020