-
Notifications
You must be signed in to change notification settings - Fork 76
TCP I O multiplexing
PAN, Myautsai edited this page Feb 4, 2016
·
4 revisions
Given command GET foo bar
, suppose we need to retrieve key "foo"
from memcached server A and key "bar"
from memcached server B. The simplest way is to retrieve them one by one:
send "GET foo\r\n" to mc A
recv from mc A
send "GET bar\r\n" to mc B
recv from mc B
This's the blocking I/O model.
When recv
from mc A is blocked by mc A but it's ready to send
to mc B, we have to wait here and doing nothing. The solution is to use I/O multiplex:
events = [
when mc A is ready to send,
when mc B is ready to send
]
not_done = 2
while not_done:
captured_events = poll(events, timeout)
for ev in captured_events:
if ev.mc is ready to send:
all_sent = ev.mc.send(...)
if all_sent:
events.remove(when ev.mc is ready to send)
events.append(when ev.mc is ready to recv)
if ev.mc is ready to recv:
all_recved = ev.mc.recv(...)
if all_recved:
events.remove(when ev.mc is ready to recv)
not_done -= 1
This's the I/O multiplexing model.
The I/O multiplexing model is used in libmc. You can find the related code here
Two images in this page are from Week11-Select