Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^3: About non-blocking sockets

by Ven'Tatsu (Deacon)
on Aug 18, 2005 at 17:09 UTC ( [id://484881]=note: print w/replies, xml ) Need Help??


in reply to Re^2: About non-blocking sockets
in thread About non-blocking sockets

Blocking is when an action will stall if it can't be completed immediately.
An example using a socket would be if you read from it when there is no data to receive my $line = <$soc>; if there is no data the program will electively wait at this bit of code until a full line of data is received then the read operation will return and the program will continue. The waiting is called blocking. It's most often a concern when reading data, but in some circumstances it can happen when writing as well.
To see blocking in effect try

print "enter some text and press enter\n"; print "enter quit to exit\n"; while (<>) { chomp; exit if $_ = quit"; print "I got $_\n"; }
In the above code the readline operator (<>) will block waiting for a line to be typed in on the console. When enter is pressed it will be read in and printed back out, then it will block again at the readline.

Buffering (controlled by $| or ->autoflush) is when input or output is collected together and stored before it moves from it's source to it's destination.
It can be though of a bit like a bucket, each time you print a cup of water is poured into the bucket. Because for the computer it takes time to empty the bucket it's only emptied when enough water is poured in to fill it up. However if it is important that the water reach it's destination in a timely fashion you can set the bucket to be emptied after each addition of water.
To see buffering try

print "sleeping: "; sleep 2; print "done\n"; sleep 1; $|=1; print "sleeping again: "; sleep 2; print "done\n"
Under most consoles (but possibly not all) you will see nothing for 2 seconds and then the full line "sleeping: done\n" will be printed out. Then a second later you will see "sleeping again: " printed out with the cursor left at the end of the line. Two seconds later "done\n" is printed. The difference is because most consoles are line buffered by default. That means they wait until they have a full line before they print any thing out. Setting $| to a true value causes this buffer to be flushed after each print statement, letting you see a partial line.

Replies are listed 'Best First'.
Re^4: About non-blocking sockets
by ivanatora (Sexton) on Aug 18, 2005 at 21:26 UTC
    I see the difference betwen blocking and buffering.
    So, the <$filehandle> operator blocks the code? Should I use read() and write(), respective fcntl instead? I prefer to stick with <$filehandle> if possible.
      Any method of reading can block if a filehandle is in blocking mode. So simply changing the function used to read the data won't help.
      If you can set non blocking mode on your socket it would let you continue using readline(a.k.a. <$filehandle>), but not all systems are created equal and some may not be as non blocking as you whould like. (For example it is my experiance that sockets could not be made compleatly non blocking on a Win98 system.)
      The alternative to non blocking IO is to poll the filehandle to see if it has data to read, or can accept data for writeing. Most event frameworks like those in GUI systems or POE handle most of the work for you. The alternative is to use the 4 arg version of select or the OO wrapper around it IO::Select.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://484881]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (5)
As of 2024-03-28 16:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found