in reply to Re^2: regarding 1.02 (was Re: IO::Lambda: call for participation)
in thread IO::Lambda: call for participation
I'm gonna take a swing at it, because I think I get it now, and someone will correct me if I'm wrong :-)
writable {...} creates an event for the IO::Lambda event loop to wait for the socket to be writable before it executes the code ref. But the actual call to writable doesn't block, if you put code after writable's closing brace, it would get executed almost immediately. One of the examples in the docs confused me at first, where there was an again;, with some code right after it. At first I was thinking that "again" was like a goto (of the next/retry sort), and the code after would never be executed, but then I realized that it was just setting up another event for IO::Lambda to wait for, and the actual call to again() would return immediately.
So when the socket is writable, you write to it in the code ref. But if you want IO::Lambda to wait for something else after you've written to the socket, it needs to be inside writable's code ref, after you've written. So that's when we call readable() to make IO::Lambda wait for the socket to be readable.
When does the closure passed to readable {} get executed? When does the code within the closure passed to writable get executed {}?
As soon as the enclosing lambda {...} thing is started, writable is executed, and the event (of waiting for $socket to be writable) is added to the event loop. The code ref is called when $socket is writable. When the code ref is called, readable is called, and the event loop will now wait for the socket to be readable (along with any other events that it's still waiting for), and when the $socket is readable, that code ref will be called.
So if writeable {..} followed readable {...} without the nesting, you'd be creating two distinct events at the same time, and it would call one code ref if the socket was readable, and the other if it was writable in whichever order the events happened.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^4: regarding 1.02 (was Re: IO::Lambda: call for participation)
by dk (Chaplain) on Jan 15, 2009 at 10:47 UTC |