Python sockets buffering -


Assume that I want to read a line from the socket using the standard socket module: / P>

  def read_line (s): ret = '' while true: c = s.recv (1) if c == '\ n' or c == '': Break Others: ret + = C return return  

what is actually in s.recv (1) ? Will this issue a system call every time? I think I should add some buffering, anyway:

For best matching with hardware and network realities, the value of bufsize is 2 Should, for example, 40 9 6

But writing efficient and thread-safe buffering does not seem easy. If I file.readline () ?

  Use #, so does it work well, does it buffer efficiently? S.makefile () Readline ()  

this recv () call c library The function is directly controlled by calling.

It will wait for the socket's data to wait. In fact it will just leave the recv () system call block.

file.readline () is a skilled buffer implementation, this is not a thread because it assumes that this file is read. (For example, by buffering incoming input.)

If you are using a file object, every time the read () is called with a positive argument, Underlying code

  • You said redline (

    ), which reads a full buffer < Li>

    The end of the line before the end of the buffer

    The way the buffer is going in otherwise the buffer is not generally higher.

    The goal of the question is not clear. If you need to see that data is available before reading, you can select () or s.setblocking (wrong) with non-blocking mode You can set socket in Then, if there is no waiting data, instead of leaving the block, the blank will come back.

    Do you read a file or socket with multiple threads?

    Give advice and advice.

I will keep a single worker at the table for reading the socket and feeding items received in the queue to deal with other threads.


Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -