问题
I'm looking at profiles for my Tornado TCP application and I'm finding that the thread running the event loop spends about 20% time in tornado/iostream.py::write_to_fd
in the line self.socket.send(data)
and about 15% time in tornado/iostream.py::read_from_fd
in the line self.socket.recv_into(buf)
.
Given that these are non-blocking sockets, why does this take any time at all? What are common situations for this to take time and are there any common optimizations I can do to my application to improve efficiency?
回答1:
Even though these are non-blocking sockets, the system calls still involve a context switch into and out of kernel mode. There's not a lot you can do to reduce this besides minimizing the number of such calls you make (e.g. by doing your reads and writes in larger batches).
来源:https://stackoverflow.com/questions/51686171/why-does-tornado-spend-time-in-socket-senddata