Home > front end >  Confusion about multithreading in Python sockets and FastAPI
Confusion about multithreading in Python sockets and FastAPI

Time:11-29

Problem:

I am trying to figure out if I really need to implement any thread safe mechanism when dealing with a client with multiple threads accessing the same (client) socket, and the information I find seems contradictory.

And as you can see, there is no explicity thread-safety mechanism.

  • However, according to this answer, sockets are not thread safe (and I have read that under some conditions there can be problems when using the same socket although nothing clear to me): Python: Socket and threads?

Context

The setup I need to deploy to production is a FastAPI server that accepts http requests, and then, as a client, makes requests to a server socket maintaining a sinlge socket regardless of http clients. My routes are not async, and that means parallel http requests from the frontend (or frontends) will be handled as different threads by FastAPI and then those threads will use the same socket to send data to the socket. The problem is that I have experienced "broken pipe" errors some times and I don't really know where the weak point of the whole setup might be. These errors occur in the client socket end of the FastAPI appplication, when handling more than one request at the same time.

In short, I would like to know if someone has suggestions in terms of thread safety for the whole setup.

CodePudding user response:

If I understand correctly you have a single FastAPI server. This server can handle multiple client connections simultaneously. Each connection has its own socket and thread. To handle requests, the FastAPI server communicates with another, back-end server. You use a single socket to communicate with the back-end server.

When listing for new client connections, there are no multi-threading issues. Only the main thread listens on the socket. When a new client connects, you get a new socket and you create a new thread.

When communicating with the client, there are no multi-threading issues. Every connection has its own socket and thread. This thread is the only thread that uses this socket.

When communicating with the back-end server, there are multi-threading issues. All the threads use the same socket. As sockets are not thread-safe, this can and will cause issues.

You need to come up with a different setup.

You can have each thread create its own socket to communicate with the back-end server. This is a simple setup but uses more resources (on both servers).

You can synchronize access to the one back-end socket so that only one thread can use the socket at any given time. This means that clients will block when another client is communicating with the back-end server.

  • Related