Email or username:

Password:

Forgot your password?
Drew DeVault

If one does not understand how blocking I/O works, it is too soon to research async I/O

17 comments
Daniel

@drewdevault may I research it concurrently, tho

Drew DeVault

A good place to start is asking yourself, "what happens if my program is not blocking on read(2) on a network socket when a packet comes in"

The answer is not that the router waits until you call read(2) to send it!

char *lotte; :v_trans: :v_bi:

@drewdevault it kinda is like that for TCP tho. well. if the kernel buffer is filled and packets get dropped the peer will resend them...

Drew DeVault

Also, I can count on one hand the number of programmers I trust to use O_NONBLOCK

cobratbq - cranky-by-design

@drewdevault io_uring magically fixes everything and makes it faster 😉

Drew DeVault

One last tip: the purpose of async I/O is to give the CPU back to your program while the I/O operation completes. But if you don't have a plan for what to do with the CPU while you have it, you don't need async I/O.

Drew DeVault

Async I/O is also often used for I/O multiplexing, which is when you have more than one source of I/O and want to have several I/O operations in flight at the same time, but otherwise you don't need the CPU while they work. You don't need async I/O for that, you need poll(2).

Drew DeVault

And finally, if your program is I/O bound then you don't need threads.

Mikołaj Hołysz

@drewdevault I'd disagree here, a program can be I/O bound (let's say small requests to a slow service / on a slow network), but you might still want to e.g. accept input from the user while the slow service is doing its thing. If you don't have threads or async i/o, you're waiting on the network while the user thinks your program is broken.

Drew DeVault

@miki bzzzzzt, wrong

You want I/O multiplexing to address this case, which is not the same thing as async I/O.

_L4NyrlfL1I0

@drewdevault how exactly does async I/O differ from I/O multiplexing in your understanding?

I/O multiplexing is IIUC having a single thread of execution and using poll() / epoll() / select() / ... to periodically check which I/O is ready and then handling that (either directly or via callbacks). Most nontrivial programs that do that do it via callbacks and an event loop.

1/2

@miki

Rich Felker

@drewdevault You don't need them for parallelism to utilize resources well. You might still want/need them for a simpler programming model.

Gary "grim" Kramlich

@drewdevault This is precisely while @pidgin is moving from sockets to the GSocket abstraction. BSD sockets are too easy to get wrong.

GezThePez

@drewdevault it’s all to do with the universal asynchronous receiver transmitter

JayT

@drewdevault I know you aren't the biggest fan of python, but when needing to do async stuff I've found the Trio library to be pretty awesome. It helps me think about doing async work in a more traceable/logical manner. I'd love to see a similar async library in other languages.

‏lol‏ ‏fedifriend (cat aspect)

@drewdevault So what you're saying is... you have to wait for your learning of blocking I/O to finish. 😁

Go Up