You can create TCP and Unix socket servers in asyncio programs.
TCP sockets are suited for network applications, allowing connections from both local and remote clients. Unix sockets can be faster than TCP sockets, although are file-based and typically limited to client connections on one machine.
In this tutorial, you will discover the difference between Asyncio TCP and Unix socket servers in Python.
Let’s get started.
How to Create an Asyncio Server
Asyncio supports the development of socket servers that allow non-blocking reading and writing with connected clients.
There are two ways to create an asyncio server, they are:
- Create a TCP Socket server.
- Create a Unix Socket server.
Let’s take a closer look at how to create each in turn.
How to Create a TCP Socket Server
We can create an asyncio TCP socket server using the asyncio.start_server() function.
This function takes many arguments that configure the server, although critically these include the client callback function, the host, and the port number.
Create a TCP server (socket type SOCK_STREAM) listening on port of the host address. Returns a Server object.
— Event Loop, Asyncio API Documentation.
For example:
1 2 3 |
... # create a tcp socket server server = await asyncio.start_server(handler, '127.0.0.1', 888) |
The server will accept stream or TCP socket connections, e.g. socket.SOCK_STREAM as opposed to datagram or UDP connections. By default, the family will be automatically chosen as either socket.AF_INET or socket.AF_INET6 (e.g. IPv4 or IPv6).
You can learn more about how to create an asyncio TCP socket server in the tutorial:
How to Create a Unix Socket Server
We can create an asyncio Unix socket server using the asyncio.start_unix_server() function.
This function takes many arguments, although critically these include a path to the file that represents the socket.
path is the name of a Unix domain socket, and is required, unless a sock argument is provided.
— Event Loop, Asyncio API Documentation.
For example:
1 2 3 |
... # create a unix socket server server = await asyncio.start_unix_server('./unix.socket') |
The server will accept socket.AF_UNIX family connections that are the stream type, e.g. socket.SOCK_STREAM.
This server can only be created on Unix and Unix-like platforms (e.g. Linux and macOS, not Windows).
Now that we know how to create TCP and Unix socket servers, let’s learn more about them.
Run loops using all CPUs, download your FREE book to learn how.
What Are Sockets
We now know that we can start two main types of socket servers in asyncio.
But what are they? Let’s take a moment to understand the two main types of socket servers.
They are:
- TCP Socket Servers
- Unix Socket Servers
Let’s review each in turn.
What Are TCP Sockets
TCP (Transmission Control Protocol) sockets are a fundamental communication mechanism in computer networks.
They provide a reliable, connection-oriented channel for data exchange between applications.
A network socket is a software structure within a network node of a computer network that serves as an endpoint for sending and receiving data across the network.
— Network socket, Wikipedia.
In a TCP socket communication, two entities, often referred to as the client and the server, establish a connection before exchanging data. The connection is established through a process known as the three-way handshake, which involves the exchange of control messages to synchronize and establish a reliable connection. Once the connection is established, data can be sent bidirectionally.
TCP sockets are extensively used in client-server architectures, where a server listens for incoming connections from clients. Web servers and browsers utilize TCP for delivering web content, ensuring that data such as HTML pages, images, and other resources are reliably transmitted. Additionally, protocols like FTP (File Transfer Protocol), SMTP (Simple Mail Transfer Protocol), and SSH (Secure Shell) rely on TCP for secure and dependable communication.
Because of the standardization of the TCP/IP protocols in the development of the Internet, the term network socket is most commonly used in the context of the Internet protocol suite, and is therefore often also referred to as Internet socket. In this context, a socket is externally identified to other hosts by its socket address, which is the triad of transport protocol, IP address, and port number.
— Network socket, Wikipedia.
One of the key features of TCP is its reliability. It guarantees the delivery of data without loss or corruption, and it ensures that data is delivered in the same order in which it was sent. This makes TCP suitable for applications where the integrity and order of data are critical, such as file transfers, remote login sessions, and database transactions.
While TCP provides a robust and dependable communication channel, it introduces some overhead due to its connection-oriented nature and additional control messages. This can result in slightly higher latency compared to connectionless protocols like UDP (User Datagram Protocol). However, the trade-off is the assurance of reliable data delivery.
You can learn more about TCP sockets in the article:
What Are Unix Sockets
UNIX sockets, also known as IPC (Inter-Process Communication) sockets or Unix Domain sockets, provide a communication mechanism between processes on the same Unix-like operating system.
A Unix domain socket aka UDS or IPC socket (inter-process communication socket) is a data communications endpoint for exchanging data between processes executing on the same host operating system. It is also referred to by its address family AF_UNIX.
— Unix domain socket, Wikipedia.
Unlike TCP sockets, UNIX sockets operate locally within a single machine, offering a fast and efficient way for processes to exchange data without the overhead associated with network communication.
UNIX sockets leverage the file system to establish communication channels between processes. They are represented as special files in the file system hierarchy, residing in the file namespace. Processes can communicate through these sockets using standard file I/O operations, such as reading and writing.
One notable advantage of UNIX sockets is their low overhead and high speed, as they operate entirely within the kernel and avoid the complexities of network protocols. This makes them ideal for communication between processes on the same machine, providing a lightweight and efficient alternative to network-based communication.
UNIX sockets are commonly used for communication between different components of a single application or between separate applications running on the same host. They are well-suited for scenarios where low latency and high throughput are crucial, such as communication between a web server and a database server on the same machine.
Security is another notable aspect of UNIX sockets. Since they operate locally, there is no exposure to network-based attacks. Access to UNIX sockets is governed by file permissions, providing a level of control over which processes can communicate through a particular socket.
You can learn more about Unix sockets in the article:
Compare Asyncio TCP vs Unix Socket Server
TCP sockets and UNIX sockets serve as fundamental communication mechanisms, each with its own set of characteristics and use cases.
Let’s compare each in turn.
TCP Sockets
TCP sockets are widely used for network communication in a distributed environment.
They provide reliable, connection-oriented communication over a network. Key features include:
- Speed: TCP sockets offer reliable, stream-oriented communication, which ensures data integrity. However, the additional features like error handling and sequencing can introduce some overhead, impacting speed compared to other protocols.
- Portability: TCP sockets are highly portable and can facilitate communication between devices over different networks. They operate on a standardized protocol, making them interoperable across various platforms and systems.
- Security: TCP itself doesn’t inherently provide encryption or security features. However, additional security measures, such as TLS/SSL, can be implemented to secure the communication channel.
UNIX Sockets
UNIX sockets provide communication channels within the same machine, typically between processes running on a Unix-like operating system. Key features include:
- Speed: UNIX sockets are generally faster than TCP sockets due to their local nature. Communication happens within the same machine, avoiding the overhead associated with network communication.
- Portability: While UNIX sockets are effective for inter-process communication (IPC) on Unix-like systems, they may not be directly portable to other operating systems. They are primarily suitable for communication within a single machine.
- Security: UNIX sockets are considered secure for local communication since they don’t expose data to external networks. However, they rely on the security mechanisms of the underlying operating system.
Comparison
- Speed: UNIX sockets are typically faster for local communication due to their proximity, while TCP sockets are designed for reliable communication over networks.
- Portability: TCP sockets are highly portable and can facilitate communication across networks and different platforms. UNIX sockets are limited to communication within a single machine.
- Security: Both can be secure, but UNIX sockets may be considered more secure for local communication, while TCP sockets require additional measures like TLS/SSL for secure network communication.
Free Python Asyncio Course
Download your FREE Asyncio PDF cheat sheet and get BONUS access to my free 7-day crash course on the Asyncio API.
Discover how to use the Python asyncio module including how to define, create, and run new coroutines and how to use non-blocking I/O.
Key Considerations Between TCP and Unix Sockets
There are additional critical considerations when choosing between TCP and UNIX sockets, depending on the specific requirements and characteristics of the application:
1. Network Boundaries:
- TCP Sockets: Ideal for communication across network boundaries. Suitable for distributed systems where processes run on different machines.
- UNIX Sockets: Limited to communication within a single machine. They are optimal for inter-process communication (IPC) on the same host.
2. Overhead and Efficiency:
- TCP Sockets: Incur higher overhead due to features like reliability, error handling, and sequencing. Efficient for communication over networks.
- UNIX Sockets: Lower overhead, making them more efficient for communication within the same machine. Suitable for scenarios where speed is critical.
3. Latency:
- TCP Sockets: May introduce additional latency compared to UNIX sockets, especially in scenarios where data needs to traverse a network.
- UNIX Sockets: Lower latency as communication happens locally, minimizing the need for network traversal.
4. Firewall Considerations:
- TCP Sockets: Require proper configuration of firewalls and network settings for communication across machines. Firewall rules need to be configured to allow traffic on specific ports.
- UNIX Sockets: Do not involve network communication, so they are not subject to firewall considerations within the machine.
5. Platform Dependency:
- TCP Sockets: Highly portable across different operating systems and platforms, making them suitable for cross-platform applications.
- UNIX Sockets: Tied to Unix-like operating systems. Applications relying on UNIX sockets may face challenges when porting to non-Unix environments.
6. Scaling and Load Balancing:
- TCP Sockets: Well-suited for scenarios where applications need to scale horizontally across multiple machines. Load balancing can be implemented at the network level.
- UNIX Sockets: More suitable for scenarios where scaling involves processes on a single machine. Load balancing may involve different strategies within the local environment.
7. Complexity and Development Overhead:
- TCP Sockets: Provide a robust and feature-rich communication model but may require additional development effort due to considerations like error handling, buffering, and connection management.
- UNIX Sockets: Simpler to implement and often involve less overhead, making them a straightforward choice for local communication.
Choosing
The choice between TCP and UNIX sockets depends on the specific requirements of the communication scenario.
It may involve trade-offs based on factors such as network boundaries, efficiency, latency, firewall considerations, platform dependency, scalability, and development complexity.
- TCP sockets are well-suited for networked environments where portability and reliability are crucial.
- UNIX sockets shine in scenarios where local communication speed and security within a single machine are the primary concerns.
Overwhelmed by the python concurrency APIs?
Find relief, download my FREE Python Concurrency Mind Maps
Further Reading
This section provides additional resources that you may find helpful.
Python Asyncio Books
- Python Asyncio Mastery, Jason Brownlee (my book!)
- Python Asyncio Jump-Start, Jason Brownlee.
- Python Asyncio Interview Questions, Jason Brownlee.
- Asyncio Module API Cheat Sheet
I also recommend the following books:
- Python Concurrency with asyncio, Matthew Fowler, 2022.
- Using Asyncio in Python, Caleb Hattingh, 2020.
- asyncio Recipes, Mohamed Mustapha Tahrioui, 2019.
Guides
APIs
- asyncio — Asynchronous I/O
- Asyncio Coroutines and Tasks
- Asyncio Streams
- Asyncio Subprocesses
- Asyncio Queues
- Asyncio Synchronization Primitives
References
Takeaways
You now know the difference between Asyncio TCP and Unix socket servers.
Did I make a mistake? See a typo?
I’m a simple humble human. Correct me, please!
Do you have any additional tips?
I’d love to hear about them!
Do you have any questions?
Ask your questions in the comments below and I will do my best to answer.
Photo by Tyler Clemmensen on Unsplash
Do you have any questions?