python--network programming, how to avoid deadlocks?

Source: Internet
Author: User

Question Description: What is a deadlock?

Deadlocks occur when a server and client attempt to write to a connection or read from a connection at the same time. In this case, no process can get any data (if they are all reading), so if they are writing, the outward buffer will be filled, and as a result they seem to have been cheated and can do nothing.

Sample server code:

ImportSocket,tracebackhost=""Port=51423sock=Socket.socket (Socket.af_inet,socket. SOCK_STREAM) sock.setsockopt (socket. Sol_socket,socket. SO_REUSEADDR,1) Sock.bind ((Host,port)) Sock.listen (1) whileTrue:Try: Clientsock,clientaddr=sock.accept ()exceptKeyboardinterrupt:Raise    except: Traceback.print_exc ()Continue    Try:        Print("Got Connection from", Clientsock.getpeername) whileTrue:data= CLIENTSOCK.RECV (4096)            if  notlen (data): Breakclientsock.sendall (data)except(keyboardinterrupt,systemexit):Raise    except: Traceback.print_exc ()Try: Clientsock.close ()exceptKeyboardinterrupt:Raise    except: Traceback.print_exc ()
View Code

Sample client code:

ImportSocketImportSysport=51423Host="localhost"Data=b"x"*10485760sock=Socket.socket (Socket.af_inet,socket. Sock_stream) Sock.connect ((host,port)) Byteswritten=0 whilebyteswritten<len (data): Startpos=Byteswritten Endpos= Min (byteswritten+1024, Len (data)) Byteswritten+=sock.send (Data[startpos:endpos]) sys.stdout.write ("wrote%d bytes\r"%Byteswritten) Sys.stdout.flush () Sock.shutdown (1)Print("All data sent.") whileTrue:buf= SOCK.RECV (1024). Decode ()if  notLen (buf): Breaksys.stdout.write (BUF)
View Code

Run the client code in the case of running the above server code, and get the following result:

server: (EV1) [[email protected] bin]# python testserver.pygot connection   from <built-inobject0x7f5e18d2b460>164864 bytes

As can be seen, the above server and client card in the wrote 164864 bytes.

-------------------------------------------------------------------Analysis---------------------------------------------------- ----------------------------------------

The client program is known to attempt to send a 10MB of data, each time it transmits 1KB, and simultaneously displays the sending data dynamically, and reads and writes data from the server every 1KB after all data is sent.

And after the server establishes the socket interface connection, reads 4KB data from the client each time, after receiving the data, sends the data directly back to the client.

Paradoxically, because the client sends a 10MB size of data, which is sent for a longer period of time, there is no way to read the data during the sending process, so the data returned by the server accumulates in the client's receive buffer.

When the receive buffer is full, the server's Sendall () function has an error, cyclic deadlock, and the server no longer receives data from the client. The client is also unable to continue sending data. The above situation is formed.

------------------------------------------------------------------How to avoid deadlocks? ----------------------------------------------------------------------------------

1, after each execution of send (), the client can recv () to receive the data sent by the server, to avoid buffer fill.

2, the client can send less data, so that buffer is full before the server can read the data sent back.

3, using multithreading or some other methods, so that clients can send and receive at the same time.

-----------------------------------------------------------------Knowledge Supplement---------------------------------------------------- --------------------------------------

Buffer:

1. TCP send and receive buffers default values

[Email protected] www.linuxidc.com]# Cat/proc/sys/net/ipv4/tcp_rmem
4096 87380 4161536

87380:tcp the default value of the receive buffer
[Email protected] www.linuxidc.com]# Cat/proc/sys/net/ipv4/tcp_wmem
4096 16384 4161536

16384:tcp default value of the Send buffer
2. TCP or UDP transmit/receive buffer maximum value


[Email protected] www.linuxidc.com]# Cat/proc/sys/net/core/rmem_max
131071

The 131071:TCP or UDP receive buffer can be half the maximum set value.
That means call setsockopt (S, Sol_socket, So_rcvbuf, &rcv_size, &optlen); If the rcv_size is more than 131071, then
GetSockOpt (S, Sol_socket, So_rcvbuf, &rcv_size, &optlen); The value to go to is equal to 131071 * 2 = 262142

[Email protected] www.linuxidc.com]# Cat/proc/sys/net/core/wmem_max
131071

The maximum set of 131071:TCP or UDP send buffers is worth half.
With the same principle as above

3. UDP transmit/Receive buffer default value

[Email protected] www.linuxidc.com]# Cat/proc/sys/net/core/rmem_default
111616:UDP the default value of the receive buffer

[Email protected] www.linuxidc.com]# Cat/proc/sys/net/core/wmem_default
111616

111616:UDP default value of the Send buffer
4. TCP or UDP transmit/receive buffer minimum value
The minimum value for the TCP or UDP receive buffer is bytes, which is determined by the kernel macro;

The minimum value for the TCP or UDP send buffer is 2048 bytes, which is determined by the kernel's macro

python--network programming, how to avoid deadlocks?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.