【Python之旅】第五篇(二):Python Socket单线程+阻塞模式

本文通过两个实例详细解析单线程+阻塞模式下,服务器如何与客户端交互及出现阻塞的原因。演示了当有多个客户端尝试连接时,由于服务器端线程被占用而发生的数据接收失败现象。

1.单线程+阻塞+交互式

    前面的例子是单线程阻塞和非交互式的,现在改写为交互式的,即不会执行一次就结束,希望达到的效果是,发送的数据由User输入,然后Server端进行接收。

Server端:与上个例子一样,并没有什么变化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import socket                #导入socket类
  
HOST =''                     #定义侦听本地地址口(多个IP地址情况下),这里表示侦听所有,也可以写成0.0.0.0
PORT = 50007                 #Server端开放的服务端口
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)    #选择Socket类型和Socket数据包类型
s.bind((HOST, PORT))         #绑定IP地址和端口
s.listen(1)                  #定义侦听数开始侦听(实际上并没有效果)
conn, addr = s.accept()      #定义实例,accept()函数的返回值可以看上面的socket函数说明
  
print 'Connected by', addr
while 1:
    data = conn.recv(1024)    #接受套接字的数据
    if not data:break         #如果没有数据接收,则断开连接
    print 'revc:',data        #发送接收到的数据
    conn.sendall(data)        #发送接收到的数据
conn.close()                      #关闭套接字

Client端:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import socket
 
HOST = '192.168.1.13'
PORT = 50007
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
 
while True:
    user_input = raw_input('msg to send:').strip()    #由User输入要发送的数据
    s.sendall(user_input)
    data = s.recv(1024)
    print 'Received', repr(data)
 
s.close()

演示:

步骤1:Server端运行服务端程序

1
2
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day5$ python server4.py 
===>光标在此处处于等待状态

步骤2:Client A端运行客户端程序

1
2
3
4
5
6
7
8
9
10
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day5$ python client4.py 
msg to send:The first msg.    ===>User输入数据
Received 'The first msg.'     ===>Server端返回的数据
msg to send:The second msg.
Received 'The second msg.'
msg to send:The third msg.
Received 'The third msg.'
msg to send:I'm A.
Received "I'm A."
msg to send:                  ===>继续等待User输入数据

步骤3:在Server端中观察现象

1
2
3
4
5
6
7
8
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day5/[2]sec_4_ver2(单线程,交互式,阻塞模
一般演示)$ python server4.py 
Connected by ('192.168.1.13'52645)
revc: The first msg.    ===>接收到用户发送的数据
revc: The second msg.
revc: The third msg.
revc: I'm A.
===>光标在此处处于等待状态

如果此时有另一个Client B端再连接进来,会有下面的情况:

1
2
3
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day5$ python client4.py 
msg to send:I'm B
===>光标在此处处于等待状态

    这时如果在Client A端断开连接,则服务端也会关闭套接字,Client B端发送的数据仍然无法被Server端接收。

    此时服务端即出现阻塞情况,因为服务端还和Client A处于连接状态,无法接收Client B发送的数据,这也说明了此时的Server端是单线程的。


2.单线程+阻塞+交互式的进阶演示

    把上面的例子中的代码再做进一步的修改,以使得阻塞模式的现象更加明显。

Server端:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import socket
 
HOST =''
PORT = 50007
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
 
while 1:
    conn, addr = s.accept()        #在循环中接受Client端连接的请求
    print 'Connected by', addr
    while True:                    #再做一个内部的循环
        data = conn.recv(1024)
        print 'Received',data
        if not data:break
        conn.sendall(data)
conn.close()

Client端:与前面例子的代码一样

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import socket
 
HOST = '192.168.1.13'
PORT = 50007
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
 
while True:
    user_input = raw_input('msg to send:').strip()
    s.sendall(user_input)
    data = s.recv(1024)
    print 'Received', repr(data)
 
s.close()

演示:

步骤1:Server端运行服务端程序

1
2
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day5$ python server4.py 
===>光标在此处处于等待状态

步骤2:Client A端运行客户端程序

1
2
3
4
5
6
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day5$ python client4.py 
msg to send:Hello!
Received 'Hello!'
msg to send:I'm Client A.
Received "I'm Client A."
msg to send:        ===>继续等待User输入数据

步骤3:在Server端中观察现象

1
2
3
4
5
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day5$ python server4.py 
Connected by ('192.168.1.13'52647)
Received Hello!
Received I'm Client A.
===>光标在此处处于等待状态

如果此时有另一个Client B端再连接进来,会有下面的情况:

1
2
3
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day5$ python client4.py 
msg to send:I'm Client B.
===>光标在此处处于等待状态

Server端的状态依然为:

1
2
3
4
5
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day5$ python server4.py 
Connected by ('192.168.1.13'52647)
Received Hello!
Received I'm Client A.
===>光标在此处处于等待状态

这时试图把Client A端断开:

1
2
3
4
5
6
7
8
9
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day5$ python client4.py 
msg to send:Hello!
Received 'Hello!'
msg to send:I'm Client A.
Received "I'm Client A."
msg to send:^CTraceback (most recent call last):
  File "client4.py", line 10in <module>
    user_input = raw_input('msg to send:').strip()
KeyboardInterrupt

再看看Server端的情况:

1
2
3
4
5
6
7
8
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day5$ python server4.py 
Connected by ('192.168.1.13'52647)
Received Hello!
Received I'm Client A.
Received 
Connected by ('192.168.1.13'52648)
Received I'm Client B.    ===>成功接收到来自Client B端发送的数据
===>光标在此处处于等待状态

再看看Client B端的情况:

1
2
3
4
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day5$ python client4.py 
msg to send:I'm Client B.
Received "I'm Client B."
msg to send:    ===>光标在此处处于等待状态

    以上的现象,再根据Server端的程序代码,就可以非常好理解单线程模式和阻塞的细节情况了,在这里是这样的:Server端接受Client A端的连接后,即把接受连接的线程释放,但此时仍然占用接收和发送数据的线程,所以Client B端虽然可以连接上Server端,但数据是无法成功被Server端接收的;当Client A端断开与Server端的连接后,Server端的接收和发送数据的线程立即被释放,之后就可以正常接收来自Client B端发送的数据了。

    


    单线程,即数据的串行发送,会导致阻塞,上面的两个例子就非常好地演示了这个阻塞的过程,如果要解决这个问题,当然在Server端就需要支持多线程,即数据折并发。




本文转自 xpleaf 51CTO博客,原文链接:http://blog.51cto.com/xpleaf/1700037,如需转载请自行联系原作者

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值