Leveraging Unix Sockets for Efficient Inter-Process Communication with aiohttp

Feb 22, 2024 ยท 2 min read

Inter-process communication (IPC) enables separate processes on the same machine to talk to each other. While web sockets and HTTP provide networking communication, sometimes processes just need to communicate locally in an efficient manner.

This is where Unix domain sockets (UDS) come in handy. The Python aiohttp library supports UDS as an alternative transport for its client and server implementations.

Why Use Unix Sockets?

Compared to localhost networking, UDS provide:

  • Faster communication - avoids network protocol overhead since UDS utilizes file IO instead of TCP/IP stack.
  • Lower latency - data doesn't have to go through the network card and TCP/IP stack.
  • Improved security - UDS file permissions restrict access unlike a port open to localhost.
  • This makes UDS great for inter-process communication within the same machine.

    aiohttp Unix Domain Socket Example

    Here is a simple aiohttp server that will handle requests on a UDS instead of a network port:

    import aiohttp
    import asyncio
    
    async def handle(request):
      name = request.match_info.get('name', "Anonymous")
      text = "Hello " + name
      return aiohttp.web.Response(text=text)
    
    app = aiohttp.web.Application()
    app.add_routes([aiohttp.web.get('/', handle)])
    
    async def start_unix_server():
      await aiohttp.web.run_app(app, path='/tmp/myapp.sock')
    
    loop = asyncio.get_event_loop()
    loop.run_until_complete(start_unix_server())
    loop.run_forever()

    To access this server from other processes, simply point your aiohttp client to the socket file path instead of a host/port URL:

    async with aiohttp.ClientSession() as session:
      async with session.get('http+unix://%2Ftmp%2Fmyapp.sock') as resp:
        print(await resp.text())

    The key things to note are:

  • Server binds to UDS file path instead of port
  • Client accesses UDS path with http+unix:// scheme
  • This allows quick and efficient IPC without the overhead of networking!

    Browse by tags:

    Browse by language:

    The easiest way to do Web Scraping

    Get HTML from any page with a simple API call. We handle proxy rotation, browser identities, automatic retries, CAPTCHAs, JavaScript rendering, etc automatically for you


    Try ProxiesAPI for free

    curl "http://api.proxiesapi.com/?key=API_KEY&url=https://example.com"

    <!doctype html>
    <html>
    <head>
        <title>Example Domain</title>
        <meta charset="utf-8" />
        <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
    ...

    X

    Don't leave just yet!

    Enter your email below to claim your free API key: