Deep Redis (10) Thread IO model

Source: Internet
Author: User

Thread IO Model

The first thing to remember is that Redis is a single-threaded thread.

Why is a single thread so fast?

All Redis data is in memory, and all operations are memory-level operations, so the speed is faster than on-disk operations. But it is also because it is single-threaded, so be careful with the time-complexity O (n) instructions.

How does a single thread handle so many concurrent client connections?

Multiplexing.

Non-blocking IO

The socket method reads and writes are blocked by default and can be set to non-blocking IO via Socket.socket (). setblocking (False) in Python.

Time Polling (multiplexing)

The problem with non-blocking IO is that thread read and write data may be returned as part of the process, when the data continues to be read, and when the data continues to be written, there should be a way to notify them.

The time polling API is to solve this problem, the early Select, later the poll, now the Epoll and Kqueque.

  #基于selectimport socketimport selectimport queueserver = Socket.socket () server.bind (' localhost ', 8000)) Server.listen () server.setblocking (0) msg = {}inputs = [server]outputs = []while true:readable, Writea ble, exceptional = select.select (inputs, outputs, inputs) for R in Readable:if R is Server:conn , addr = R.accept () conn.setblocking (0) inputs.append (conn) msg[conn] = queue. Queue () Else:try:data = R.recv (1024x768) msg[r].put (data) out  Puts.append (R) except ConnectionResetError:inputs.remove (R) continue for W in Writeable:data = Msg[w].get () w.send (data) Outputs.remove (W) for E in Exceptional:if e i n Outputs:outputs.remove (E) inputs.remove (e) del msg[e]  
# 自动匹配版select,在linux中自动使用epoll,在windows中只能select,在bsd或mac中自动kqueueimport socketimport selectorsserver = socket.socket()server.bind((‘localhost‘, 8000))server.listen()server.setblocking(0)sel = selectors.DefaultSlector()def accept(server, mask):    conn, addr = server.accept()    conn.setblocking(0)    sel.register(conn, mask, read)def read(conn, mask):    data = conn.recv(1024)    if data:        conn.send(data)    else:        sel.unregister(conn)        conn.close()sel.register(server, selectors.EVENT_READ, accept)while True:    events = sel.select()    for key, mask in events:        callback = key.data        obj = key.fileobj        callback(obj, mask)

But none of this is going to happen, and Redis has implemented this polling mechanism for us, as long as we remember that multiplexing is non-blocking IO, not an asynchronous operation.

Deep Redis (10) Thread IO model

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.