[파이썬] 블로킹 vs. 논블로킹 소켓

When developing network applications in Python, one of the important considerations is how to handle socket operations efficiently. This is where the concepts of blocking and non-blocking sockets come into play.

Blocking Sockets

In Python, blocking sockets are the default behavior. When you perform any I/O operation with a blocking socket, the program execution will pause until the operation completes. Whether you are reading from or writing to the socket, the program will wait until the operation finishes before moving on to the next line of code.

import socket

# Create a blocking socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Connect to a server
sock.connect(("example.com", 80))

# Send data through the socket
sock.send(b"Hello, world!")

# Receive data from the socket
response = sock.recv(1024)

# Close the socket
sock.close()

In the above code, each I/O operation (connect, send, recv) will block until it completes. This means that if the connection or data transfer takes a long time, the program will be stuck waiting at that line of code.

Non-blocking Sockets

On the other hand, non-blocking sockets allow the program to continue executing without waiting for I/O operations to complete. Instead of blocking, non-blocking sockets return immediately with a result indicating whether the operation succeeded, failed, or is still in progress.

To use non-blocking sockets in Python, you need to set the socket object as non-blocking using the setblocking() method.

import socket

# Create a non-blocking socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setblocking(False)

# Connect to a server
# This will return immediately, regardless of whether the connection is established or not
sock.connect_ex(("example.com", 80))

# Send data through the socket
# This will also return immediately, indicating the success or failure of the operation
sock.send(b"Hello, world!")

# Receive data from the socket
# Similarly, this will return immediately, indicating whether any data is available or not
response = sock.recv(1024)

# Close the socket
sock.close()

In the code above, non-blocking sockets are achieved by calling setblocking(False) on the socket object. This allows the connect, send, and recv operations to return immediately, allowing the program to continue executing.

Choosing between Blocking and Non-blocking Sockets

The choice between blocking and non-blocking sockets depends on the requirements of your application. Here are some points to consider:

Choosing the right socket model depends on the specific needs of your application. If you need to handle multiple connections concurrently, or if you want to perform other tasks alongside socket operations, non-blocking sockets may be the better choice. However, if simplicity and ease of use are more important, blocking sockets can suffice.

Regardless of your choice, Python provides the flexibility to use either approach based on your application’s requirements.