Python开发【Part 12】:协程与IO操做模式

本节内容

  1. Python协程
  2. Select\Poll\Epoll异步IO与事件驱动
  3. IO多路复用

 

1、Python协程

  协程,又称微线程,纤程。英文名Coroutine。一句话说明什么是线程:协程是一种用户态的轻量级线程python

  协程拥有本身的寄存器上下文和栈。协程调度切换时,将寄存器上下文和栈保存到其余地方,在切回来的时候,恢复先前保存的寄存器上下文和栈。所以:linux

  协程能保留上一次调用时的状态(即全部局部状态的一个特定组合),每次过程重入时,就至关于进入上一次调用的状态,换种说法:进入上一次离开时所处逻辑流的位置。git

  协程的优势程序员

  • 无需线程上下文切换的开销
  • 无需原子操做锁定及同步的开销
    • "原子操做(atomic operation)是不须要synchronized",所谓原子操做是指不会被线程调度机制打断的操做;这种操做一旦开始,就一直运行到结束,中间不会有任何 context switch (切换到另外一个线程)。原子操做能够是一个步骤,也能够是多个操做步骤,可是其顺序是不能够被打乱,或者切割掉只执行部分。视做总体是原子性的核心。
  • 方便切换控制流,简化编程模型
  • 高并发+高扩展性+低成本:一个CPU支持上万的协程都不是问题。因此很适合用于高并发处理。

  协程的缺点:github

  • 没法利用多核资源:协程的本质是个单线程,它不能同时将 单个CPU 的多个核用上,协程须要和进程配合才能运行在多CPU上.固然咱们平常所编写的绝大部分应用都没有这个必要,除非是cpu密集型应用。
  • 进行阻塞(Blocking)操做(如IO时)会阻塞掉整个程序

使用yield实现协程操做例子    web

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import  time
import  queue
def  consumer(name):
     print ( "--->starting eating baozi..." )
     while  True :
         new_baozi  =  yield
         print ( "[%s] is eating baozi %s"  %  (name,new_baozi))
         #time.sleep(1)
 
def  producer():
 
     =  con.__next__()
     =  con2.__next__()
     =  0
     while  n <  5 :
         + = 1
         con.send(n)
         con2.send(n)
         print ( "\033[32;1m[producer]\033[0m is making baozi %s"  % n )
 
 
if  __name__  = =  '__main__' :
     con  =  consumer( "c1" )
     con2  =  consumer( "c2" )
     =  producer()

  协程一个标准定义,即符合什么条件就能称之为协程:编程

  1. 必须在只有一个单线程里实现并发
  2. 修改共享数据不需加锁
  3. 用户程序里本身保存多个控制流的上下文栈
  4. 一个协程遇到IO操做自动切换到其它协程

  基于上面这4点定义,咱们刚才用yield实现的程并不能算是合格的线程,由于它有一点功能没实现,哪一点呢?数组

Greenlet

  greenlet是一个用C实现的协程模块,相比与python自带的yield,它可使你在任意函数之间随意切换,而不需把这个函数先声明为generator缓存

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# -*- coding:utf-8 -*-
 
 
from  greenlet  import  greenlet
 
 
def  test1():
     print ( 12 )
     gr2.switch()
     print ( 34 )
     gr2.switch()
 
 
def  test2():
     print ( 56 )
     gr1.switch()
     print ( 78 )
 
 
gr1  =  greenlet(test1)
gr2  =  greenlet(test2)
gr1.switch()

  感受确实用着比generator还简单了呢,但好像尚未解决一个问题,就是遇到IO操做,自动切换,对不对?安全

Gevent 

  Gevent 是一个第三方库,能够轻松经过gevent实现并发同步或异步编程,在gevent中用到的主要模式是Greenlet, 它是以C扩展模块形式接入Python的轻量级协程。 Greenlet所有运行在主程序操做系统进程的内部,但它们被协做式地调度。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import  gevent
 
def  func1():
     print ( '\033[31;1m李闯在跟海涛搞...\033[0m' )
     gevent.sleep( 2 )
     print ( '\033[31;1m李闯又回去跟继续跟海涛搞...\033[0m' )
 
def  func2():
     print ( '\033[32;1m李闯切换到了跟海龙搞...\033[0m' )
     gevent.sleep( 1 )
     print ( '\033[32;1m李闯搞完了海涛,回来继续跟海龙搞...\033[0m' )
 
 
gevent.joinall([
     gevent.spawn(func1),
     gevent.spawn(func2),
     #gevent.spawn(func3),
])

同步与异步的性能区别 

 
import gevent
 
def task(pid):
    """
    Some non-deterministic task
    """
    gevent.sleep(0.5)
    print('Task %s done' % pid)
 
def synchronous():
    for i in range(1,10):
        task(i)
 
def asynchronous():
    threads = [gevent.spawn(task, i) for i in range(10)]
    gevent.joinall(threads)
 
print('Synchronous:')
synchronous()
 
print('Asynchronous:')
asynchronous()
View Code

上面程序的重要部分是将task函数封装到Greenlet内部线程的gevent.spawn。 初始化的greenlet列表存放在数组threads中,此数组被传给gevent.joinall 函数,后者阻塞当前流程,并执行全部给定的greenlet。执行流程只会在 全部greenlet执行完后才会继续向下走。  

遇到IO阻塞时会自动切换任务

 
from gevent import monkey; monkey.patch_all()
import gevent
from  urllib.request import urlopen
 
def f(url):
    print('GET: %s' % url)
    resp = urlopen(url)
    data = resp.read()
    print('%d bytes received from %s.' % (len(data), url))
 
gevent.joinall([
        gevent.spawn(f, 'https://www.python.org/'),
        gevent.spawn(f, 'https://www.yahoo.com/'),
        gevent.spawn(f, 'https://github.com/'),
])
View Code

经过gevent实现单线程下的多socket并发

  server side 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import  sys
import  socket
import  time
import  gevent
 
from  gevent  import  socket,monkey
monkey.patch_all()
 
 
def  server(port):
     =  socket.socket()
     s.bind(( '0.0.0.0' , port))
     s.listen( 500 )
     while  True :
         cli, addr  =  s.accept()
         gevent.spawn(handle_request, cli)
 
 
 
def  handle_request(conn):
     try :
         while  True :
             data  =  conn.recv( 1024 )
             print ( "recv:" , data)
             conn.send(data)
             if  not  data:
                 conn.shutdown(socket.SHUT_WR)
 
     except  Exception as  ex:
         print (ex)
     finally :
         conn.close()
if  __name__  = =  '__main__' :
     server( 8001 )

  client side   

 
import socket
 
HOST = 'localhost'    # The remote host
PORT = 8001           # The same port as used by the server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
while True:
    msg = bytes(input(">>:"),encoding="utf8")
    s.sendall(msg)
    data = s.recv(1024)
    #print(data)
 
    print('Received', repr(data))
s.close()
View Code
 
import socket
import threading

def sock_conn():

    client = socket.socket()

    client.connect(("localhost",8001))
    count = 0
    while True:
        #msg = input(">>:").strip()
        #if len(msg) == 0:continue
        client.send( ("hello %s" %count).encode("utf-8"))

        data = client.recv(1024)

        print("[%s]recv from server:" % threading.get_ident(),data.decode()) #结果
        count +=1
    client.close()


for i in range(100):
    t = threading.Thread(target=sock_conn)
    t.start()
并发100个socket

 

2、Select\Poll\Epoll异步IO与事件驱动

一般,咱们写服务器处理模型的程序时,有如下几种模型:
  (1)每收到一个请求,建立一个新的进程,来处理该请求;
  (2)每收到一个请求,建立一个新的线程,来处理该请求;
  (3)每收到一个请求,放入一个事件列表,让主进程经过非阻塞I/O方式来处理请求
上面的几种方式,各有千秋,
  第(1)中方法,因为建立新的进程的开销比较大,因此,会致使服务器性能比较差,但实现比较简单。
  第(2)种方式,因为要涉及到线程的同步,有可能会面临 死锁等问题。
  第(3)种方式,在写应用程序代码时,逻辑比前面两种都复杂。
综合考虑各方面因素,通常广泛认为第(3)种方式是大多数 网络服务器采用的方式

看图说话讲事件驱动模型

在UI编程中,经常要对鼠标点击进行相应,首先如何得到鼠标点击呢?
方式一:建立一个线程,该线程一直循环检测是否有鼠标点击,那么这个方式有如下几个缺点
1. CPU资源浪费,可能鼠标点击的频率很是小,可是扫描线程仍是会一直循环检测,这会形成不少的CPU资源浪费;若是扫描鼠标点击的接口是阻塞的呢?
2. 若是是堵塞的,又会出现下面这样的问题,若是咱们不但要扫描鼠标点击,还要扫描键盘是否按下,因为扫描鼠标时被堵塞了,那么可能永远不会去扫描键盘;
3. 若是一个循环须要扫描的设备很是多,这又会引来响应时间的问题;
因此,该方式是很是很差的。

方式二:就是事件驱动模型
目前大部分的UI编程都是事件驱动模型,如不少UI平台都会提供onClick()事件,这个事件就表明鼠标按下事件。事件驱动模型大致思路以下:
1. 有一个事件(消息)队列;
2. 鼠标按下时,往这个队列中增长一个点击事件(消息);
3. 有个循环,不断从队列取出事件,根据不一样的事件,调用不一样的函数,如onClick()、onKeyDown()等;
4. 事件(消息)通常都各自保存各自的处理函数指针,这样,每一个消息都有独立的处理函数;

  事件驱动编程是一种编程范式,这里程序的执行流由外部事件来决定。它的特色是包含一个事件循环,当外部事件发生时使用回调机制来触发相应的处理。另外两种常见的编程范式是(单线程)同步以及多线程编程。

  让咱们用例子来比较和对比一下单线程、多线程以及事件驱动编程模型。下图展现了随着时间的推移,这三种模式下程序所作的工做。这个程序有3个任务须要完成,每一个任务都在等待I/O操做时阻塞自身。阻塞在I/O操做上所花费的时间已经用灰色框标示出来了。

 

  在单线程同步模型中,任务按照顺序执行。若是某个任务由于I/O而阻塞,其余全部的任务都必须等待,直到它完成以后它们才能依次执行。这种明确的执行顺序和串行化处理的行为是很容易推断得出的。若是任务之间并无互相依赖的关系,但仍然须要互相等待的话这就使得程序没必要要的下降了运行速度。

  在多线程版本中,这3个任务分别在独立的线程中执行。这些线程由操做系统来管理,在多处理器系统上能够并行处理,或者在单处理器系统上交错执行。这使得当某个线程阻塞在某个资源的同时其余线程得以继续执行。与完成相似功能的同步程序相比,这种方式更有效率,但程序员必须写代码来保护共享资源,防止其被多个线程同时访问。多线程程序更加难以推断,由于这类程序不得不经过线程同步机制如锁、可重入函数、线程局部存储或者其余机制来处理线程安全问题,若是实现不当就会致使出现微妙且使人痛不欲生的bug。

  在事件驱动版本的程序中,3个任务交错执行,但仍然在一个单独的线程控制中。当处理I/O或者其余昂贵的操做时,注册一个回调到事件循环中,而后当I/O操做完成时继续执行。回调描述了该如何处理某个事件。事件循环轮询全部的事件,当事件到来时将它们分配给等待处理事件的回调函数。这种方式让程序尽量的得以执行而不须要用到额外的线程。事件驱动型程序比多线程程序更容易推断出行为,由于程序员不须要关心线程安全问题。

当咱们面对以下的环境时,事件驱动模型一般是一个好的选择:

  1. 程序中有许多任务,并且…
  2. 任务之间高度独立(所以它们不须要互相通讯,或者等待彼此)并且…
  3. 在等待事件到来时,某些任务会阻塞。

当应用程序须要在任务间共享可变的数据时,这也是一个不错的选择,由于这里不须要采用同步处理。

网络应用程序一般都有上述这些特色,这使得它们可以很好的契合事件驱动编程模型。

此处要提出一个问题,就是,上面的事件驱动模型中,只要一遇到IO就注册一个事件,而后主程序就能够继续干其它的事情了,只到io处理完毕后,继续恢复以前中断的任务,这本质上是怎么实现的呢?哈哈,下面咱们就来一块儿揭开这神秘的面纱。。。。

Select\Poll\Epoll异步IO 

首先列一下,sellect、poll、epoll三者的区别 

select 
select最先于1983年出如今4.2BSD中,它经过一个select()系统调用来监视多个文件描述符的数组,当select()返回后,该数组中就绪的文件描述符便会被内核修改标志位,使得进程能够得到这些文件描述符从而进行后续的读写操做。

select目前几乎在全部的平台上支持,其良好跨平台支持也是它的一个优势,事实上从如今看来,这也是它所剩很少的优势之一。

select的一个缺点在于单个进程可以监视的文件描述符的数量存在最大限制,在Linux上通常为1024,不过能够经过修改宏定义甚至从新编译内核的方式提高这一限制。

另外,select()所维护的存储大量文件描述符的数据结构,随着文件描述符数量的增大,其复制的开销也线性增加。同时,因为网络响应时间的延迟使得大量TCP链接处于非活跃状态,但调用select()会对全部socket进行一次线性扫描,因此这也浪费了必定的开销。

poll 
poll在1986年诞生于System V Release 3,它和select在本质上没有多大差异,可是poll没有最大文件描述符数量的限制。

poll和select一样存在一个缺点就是,包含大量文件描述符的数组被总体复制于用户态和内核的地址空间之间,而不论这些文件描述符是否就绪,它的开销随着文件描述符数量的增长而线性增大。

另外,select()和poll()将就绪的文件描述符告诉进程后,若是进程没有对其进行IO操做,那么下次调用select()和poll()的时候将再次报告这些文件描述符,因此它们通常不会丢失就绪的消息,这种方式称为水平触发(Level Triggered)。

epoll 
直到Linux2.6才出现了由内核直接支持的实现方法,那就是epoll,它几乎具有了以前所说的一切优势,被公认为Linux2.6下性能最好的多路I/O就绪通知方法。

epoll能够同时支持水平触发和边缘触发(Edge Triggered,只告诉进程哪些文件描述符刚刚变为就绪状态,它只说一遍,若是咱们没有采起行动,那么它将不会再次告知,这种方式称为边缘触发),理论上边缘触发的性能要更高一些,可是代码实现至关复杂。

epoll一样只告知那些就绪的文件描述符,并且当咱们调用epoll_wait()得到就绪文件描述符时,返回的不是实际的描述符,而是一个表明就绪描述符数量的值,你只须要去epoll指定的一个数组中依次取得相应数量的文件描述符便可,这里也使用了内存映射(mmap)技术,这样便完全省掉了这些文件描述符在系统调用时复制的开销。

另外一个本质的改进在于epoll采用基于事件的就绪通知方式。在select/poll中,进程只有在调用必定的方法后,内核才对全部监视的文件描述符进行扫描,而epoll事先经过epoll_ctl()来注册一个文件描述符,一旦基于某个文件描述符就绪时,内核会采用相似callback的回调机制,迅速激活这个文件描述符,当进程调用epoll_wait()时便获得通知。

Python select 

Python的select()方法直接调用操做系统的IO接口,它监控sockets,open files, and pipes(全部带fileno()方法的文件句柄)什么时候变成readable 和writeable, 或者通讯错误,select()使得同时监控多个链接变的简单,而且这比写一个长循环来等待和监控多客户端链接要高效,由于select直接经过操做系统提供的C的网络接口进行操做,而不是经过Python的解释器。

注意:Using Python’s file objects with select() works for Unix, but is not supported under Windows.

接下来经过echo server例子要以了解select 是如何经过单进程实现同时处理多个非阻塞的socket链接的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import  select
import  socket
import  sys
import  Queue
 
# Create a TCP/IP socket
server  =  socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.setblocking( 0 )
 
# Bind the socket to the port
server_address  =  ( 'localhost' 10000 )
print  >>sys.stderr,  'starting up on %s port %s'  %  server_address
server.bind(server_address)
 
# Listen for incoming connections
server.listen( 5 )

select()方法接收并监控3个通讯列表, 第一个是全部的输入的data,就是指外部发过来的数据,第2个是监控和接收全部要发出去的data(outgoing data),第3个监控错误信息,接下来咱们须要建立2个列表来包含输入和输出信息来传给select().

# Sockets from which we expect to read
inputs = [ server ] # Sockets to which we expect to write outputs = [ ]

全部客户端的进来的链接和数据将会被server的主循环程序放在上面的list中处理,咱们如今的server端须要等待链接可写(writable)以后才能过来,而后接收数据并返回(所以不是在接收到数据以后就马上返回),由于每一个链接要把输入或输出的数据先缓存到queue里,而后再由select取出来再发出去。

Connections are added to and removed from these lists by the server main loop. Since this version of the server is going to wait for a socket to become writable before sending any data (instead of immediately sending the reply), each output connection needs a queue to act as a buffer for the data to be sent through it.

# Outgoing message queues (socket:Queue)
message_queues = {}

The main portion of the server program loops, calling select() to block and wait for network activity.

下面是此程序的主循环,调用select()时会阻塞和等待直到新的链接和数据进来

while inputs: # Wait for at least one of the sockets to be ready for processing print >>sys.stderr, '\nwaiting for the next event' readable, writable, exceptional = select.select(inputs, outputs, inputs) 

 当你把inputs,outputs,exceptional(这里跟inputs共用)传给select()后,它返回3个新的list,咱们上面将他们分别赋值为readable,writable,exceptional, 全部在readable list中的socket链接表明有数据可接收(recv),全部在writable list中的存放着你能够对其进行发送(send)操做的socket链接,当链接通讯出现error时会把error写到exceptional列表中。

select() returns three new lists, containing subsets of the contents of the lists passed in. All of the sockets in the readable list have incoming data buffered and available to be read. All of the sockets in the writable list have free space in their buffer and can be written to. The sockets returned in exceptional have had an error (the actual definition of “exceptional condition” depends on the platform).

Readable list 中的socket 能够有3种可能状态,第一种是若是这个socket是main "server" socket,它负责监听客户端的链接,若是这个main server socket出如今readable里,那表明这是server端已经ready来接收一个新的链接进来了,为了让这个main server能同时处理多个链接,在下面的代码里,咱们把这个main server的socket设置为非阻塞模式。

The “readable” sockets represent three possible cases. If the socket is the main “server” socket, the one being used to listen for connections, then the “readable” condition means it is ready to accept another incoming connection. In addition to adding the new connection to the list of inputs to monitor, this section sets the client socket to not block.

1
2
3
4
5
6
7
8
9
10
11
12
# Handle inputs
for  in  readable:
 
     if  is  server:
         # A "readable" server socket is ready to accept a connection
         connection, client_address  =  s.accept()
         print  >>sys.stderr,  'new connection from' , client_address
         connection.setblocking( 0 )
         inputs.append(connection)
 
         # Give the connection a queue for data we want to send
         message_queues[connection]  =  Queue.Queue()

 

第二种状况是这个socket是已经创建了的链接,它把数据发了过来,这个时候你就能够经过recv()来接收它发过来的数据,而后把接收到的数据放到queue里,这样你就能够把接收到的数据再传回给客户端了。

The next case is an established connection with a client that has sent data. The data is read with recv(), then placed on the queue so it can be sent through the socket and back to the client.

1
2
3
4
5
6
7
8
9
else :
      data  =  s.recv( 1024 )
      if  data:
          # A readable client socket has data
          print  >>sys.stderr,  'received "%s" from %s'  %  (data, s.getpeername())
          message_queues[s].put(data)
          # Add output channel for response
          if  not  in  outputs:
              outputs.append(s)

第三种状况就是这个客户端已经断开了,因此你再经过recv()接收到的数据就为空了,因此这个时候你就能够把这个跟客户端的链接关闭了。

A readable socket without data available is from a client that has disconnected, and the stream is ready to be closed.

1
2
3
4
5
6
7
8
9
10
11
else :
     # Interpret empty result as closed connection
     print  >>sys.stderr,  'closing' , client_address,  'after reading no data'
     # Stop listening for input on the connection
     if  in  outputs:
         outputs.remove(s)   #既然客户端都断开了,我就不用再给它返回数据了,因此这时候若是这个客户端的链接对象还在outputs列表中,就把它删掉
     inputs.remove(s)     #inputs中也删除掉
     s.close()            #把这个链接关闭掉
 
     # Remove message queue
     del  message_queues[s]   

对于writable list中的socket,也有几种状态,若是这个客户端链接在跟它对应的queue里有数据,就把这个数据取出来再发回给这个客户端,不然就把这个链接从output list中移除,这样下一次循环select()调用时检测到outputs list中没有这个链接,那就会认为这个链接还处于非活动状态

There are fewer cases for the writable connections. If there is data in the queue for a connection, the next message is sent. Otherwise, the connection is removed from the list of output connections so that the next time through the loop select() does not indicate that the socket is ready to send data.

1
2
3
4
5
6
7
8
9
10
11
# Handle outputs
for  in  writable:
     try :
         next_msg  =  message_queues[s].get_nowait()
     except  Queue.Empty:
         # No messages waiting so stop checking for writability.
         print  >>sys.stderr,  'output queue for' , s.getpeername(),  'is empty'
         outputs.remove(s)
     else :
         print  >>sys.stderr,  'sending "%s" to %s'  %  (next_msg, s.getpeername())
         s.send(next_msg)

最后,若是在跟某个socket链接通讯过程当中出了错误,就把这个链接对象在inputs\outputs\message_queue中都删除,再把链接关闭掉

1
2
3
4
5
6
7
8
9
10
11
# Handle "exceptional conditions"
for  in  exceptional:
     print  >>sys.stderr,  'handling exceptional condition for' , s.getpeername()
     # Stop listening for input on the connection
     inputs.remove(s)
     if  in  outputs:
         outputs.remove(s)
     s.close()
 
     # Remove message queue
     del  message_queues[s]

客户端

下面的这个是客户端程序展现了如何经过select()对socket进行管理并与多个链接同时进行交互,

The example client program uses two sockets to demonstrate how the server with select() manages multiple connections at the same time. The client starts by connecting each TCP/IP socket to the server.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import  socket
import  sys
 
messages  =  'This is the message. ' ,
              'It will be sent ' ,
              'in parts.' ,
              ]
server_address  =  ( 'localhost' 10000 )
 
# Create a TCP/IP socket
socks  =  [ socket.socket(socket.AF_INET, socket.SOCK_STREAM),
           socket.socket(socket.AF_INET, socket.SOCK_STREAM),
           ]
 
# Connect the socket to the port where the server is listening
print  >>sys.stderr,  'connecting to %s port %s'  %  server_address
for  in  socks:
     s.connect(server_address)

接下来经过循环经过每一个socket链接给server发送和接收数据。

 
     

Then it sends one pieces of the message at a time via each socket, and reads all responses available after writing new data.

1
2
3
4
5
6
7
8
9
10
11
12
13
for  message  in  messages:
 
     # Send messages on both sockets
     for  in  socks:
         print  >>sys.stderr,  '%s: sending "%s"'  %  (s.getsockname(), message)
         s.send(message)
 
     # Read responses on both sockets
     for  in  socks:
         data  =  s.recv( 1024 )
         print  >>sys.stderr,  '%s: received "%s"'  %  (s.getsockname(), data)
         if  not  data:
             print  >>sys.stderr,  'closing socket' , s.getsockname()

 最后服务器端的完整代码以下: 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#_*_coding:utf-8_*_
__author__  =  'Alex Li'
 
import  select
import  socket
import  sys
import  queue
 
# Create a TCP/IP socket
server  =  socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.setblocking( False )
 
# Bind the socket to the port
server_address  =  ( 'localhost' 10000 )
print (sys.stderr,  'starting up on %s port %s'  %  server_address)
server.bind(server_address)
 
# Listen for incoming connections
server.listen( 5 )
 
# Sockets from which we expect to read
inputs  =  [ server ]
 
# Sockets to which we expect to write
outputs  =  [ ]
 
message_queues  =  {}
while  inputs:
 
     # Wait for at least one of the sockets to be ready for processing
     print '\nwaiting for the next event' )
     readable, writable, exceptional  =  select.select(inputs, outputs, inputs)
     # Handle inputs
     for  in  readable:
 
         if  is  server:
             # A "readable" server socket is ready to accept a connection
             connection, client_address  =  s.accept()
             print ( 'new connection from' , client_address)
             connection.setblocking( False )
             inputs.append(connection)
 
             # Give the connection a queue for data we want to send
             message_queues[connection]  =  queue.Queue()
         else :
             data  =  s.recv( 1024 )
             if  data:
                 # A readable client socket has data
                 print (sys.stderr,  'received "%s" from %s'  %  (data, s.getpeername()) )
                 message_queues[s].put(data)
                 # Add output channel for response
                 if  not  in  outputs:
                     outputs.append(s)
             else :
                 # Interpret empty result as closed connection
                 print ( 'closing' , client_address,  'after reading no data' )
                 # Stop listening for input on the connection
                 if  in  outputs:
                     outputs.remove(s)   #既然客户端都断开了,我就不用再给它返回数据了,因此这时候若是这个客户端的链接对象还在outputs列表中,就把它删掉
                 inputs.remove(s)     #inputs中也删除掉
                 s.close()            #把这个链接关闭掉
 
                 # Remove message queue
                 del  message_queues[s]
     # Handle outputs
     for  in  writable:
         try :
             next_msg  =  message_queues[s].get_nowait()
         except  queue.Empty:
             # No messages waiting so stop checking for writability.
             print ( 'output queue for' , s.getpeername(),  'is empty' )
             outputs.remove(s)
         else :
             print 'sending "%s" to %s'  %  (next_msg, s.getpeername()))
             s.send(next_msg)
     # Handle "exceptional conditions"
     for  in  exceptional:
         print ( 'handling exceptional condition for' , s.getpeername() )
         # Stop listening for input on the connection
         inputs.remove(s)
         if  in  outputs:
             outputs.remove(s)
         s.close()
 
         # Remove message queue
         del  message_queues[s]
1
<strong style = "font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; line-height: 1.5; " >客户端的完整代码以下:< / strong>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
__author__  =  'jieli'
import  socket
import  sys
 
messages  =  'This is the message. ' ,
              'It will be sent ' ,
              'in parts.' ,
              ]
server_address  =  ( 'localhost' 10000 )
 
# Create a TCP/IP socket
socks  =  [ socket.socket(socket.AF_INET, socket.SOCK_STREAM),
           socket.socket(socket.AF_INET, socket.SOCK_STREAM),
           ]
 
# Connect the socket to the port where the server is listening
print  >>sys.stderr,  'connecting to %s port %s'  %  server_address
for  in  socks:
     s.connect(server_address)
 
for  message  in  messages:
 
     # Send messages on both sockets
     for  in  socks:
         print  >>sys.stderr,  '%s: sending "%s"'  %  (s.getsockname(), message)
         s.send(message)
 
     # Read responses on both sockets
     for  in  socks:
         data  =  s.recv( 1024 )
         print  >>sys.stderr,  '%s: received "%s"'  %  (s.getsockname(), data)
         if  not  data:
             print  >>sys.stderr,  'closing socket' , s.getsockname()
             s.close()

Run the server in one window and the client in another. The output will look like this, with different port numbers.

$ python ./select_echo_server.py
starting up on localhost port 10000

waiting for the next event
new connection from ('127.0.0.1', 55821)

waiting for the next event
new connection from ('127.0.0.1', 55822)
received "This is the message. " from ('127.0.0.1', 55821)

waiting for the next event
sending "This is the message. " to ('127.0.0.1', 55821)

waiting for the next event
output queue for ('127.0.0.1', 55821) is empty

waiting for the next event
received "This is the message. " from ('127.0.0.1', 55822)

waiting for the next event
sending "This is the message. " to ('127.0.0.1', 55822)

waiting for the next event
output queue for ('127.0.0.1', 55822) is empty

waiting for the next event
received "It will be sent " from ('127.0.0.1', 55821)
received "It will be sent " from ('127.0.0.1', 55822)

waiting for the next event
sending "It will be sent " to ('127.0.0.1', 55821)
sending "It will be sent " to ('127.0.0.1', 55822)

waiting for the next event
output queue for ('127.0.0.1', 55821) is empty
output queue for ('127.0.0.1', 55822) is empty

waiting for the next event
received "in parts." from ('127.0.0.1', 55821)
received "in parts." from ('127.0.0.1', 55822)

waiting for the next event
sending "in parts." to ('127.0.0.1', 55821)
sending "in parts." to ('127.0.0.1', 55822)

waiting for the next event
output queue for ('127.0.0.1', 55821) is empty
output queue for ('127.0.0.1', 55822) is empty

waiting for the next event
closing ('127.0.0.1', 55822) after reading no data
closing ('127.0.0.1', 55822) after reading no data

waiting for the next event

The client output shows the data being sent and received using both sockets.

$ python ./select_echo_multiclient.py
connecting to localhost port 10000
('127.0.0.1', 55821): sending "This is the message. "
('127.0.0.1', 55822): sending "This is the message. "
('127.0.0.1', 55821): received "This is the message. "
('127.0.0.1', 55822): received "This is the message. "
('127.0.0.1', 55821): sending "It will be sent "
('127.0.0.1', 55822): sending "It will be sent "
('127.0.0.1', 55821): received "It will be sent "
('127.0.0.1', 55822): received "It will be sent "
('127.0.0.1', 55821): sending "in parts."
('127.0.0.1', 55822): sending "in parts."
('127.0.0.1', 55821): received "in parts."
('127.0.0.1', 55822): received "in parts." 


3、IO多路复用

同步IO和异步IO,阻塞IO和非阻塞IO分别是什么,到底有什么区别?不一样的人在不一样的上下文下给出的答案是不一样的。因此先限定一下本文的上下文。

本文讨论的背景是Linux环境下的network IO。

概念说明

在进行解释以前,首先要说明几个概念:
- 用户空间和内核空间
- 进程切换
- 进程的阻塞
- 文件描述符
- 缓存 I/O

用户空间与内核空间

如今操做系统都是采用虚拟存储器,那么对32位操做系统而言,它的寻址空间(虚拟存储空间)为4G(2的32次方)。操做系统的核心是内核,独立于普通的应用程序,能够访问受保护的内存空间,也有访问底层硬件设备的全部权限。为了保证用户进程不能直接操做内核(kernel),保证内核的安全,操心系统将虚拟空间划分为两部分,一部分为内核空间,一部分为用户空间。针对linux操做系统而言,将最高的1G字节(从虚拟地址0xC0000000到0xFFFFFFFF),供内核使用,称为内核空间,而将较低的3G字节(从虚拟地址0x00000000到0xBFFFFFFF),供各个进程使用,称为用户空间。

进程切换

为了控制进程的执行,内核必须有能力挂起正在CPU上运行的进程,并恢复之前挂起的某个进程的执行。这种行为被称为进程切换。所以能够说,任何进程都是在操做系统内核的支持下运行的,是与内核紧密相关的。

从一个进程的运行转到另外一个进程上运行,这个过程当中通过下面这些变化:
1. 保存处理机上下文,包括程序计数器和其余寄存器。
2. 更新PCB信息。

3. 把进程的PCB移入相应的队列,如就绪、在某事件阻塞等队列。
4. 选择另外一个进程执行,并更新其PCB。
5. 更新内存管理的数据结构。
6. 恢复处理机上下文。

总而言之就是很耗资源,具体的能够参考这篇文章:进程切换

注:进程控制块(Processing Control Block),是操做系统核心中一种数据结构,主要表示进程状态。其做用是使一个在多道程序环境下不能独立运行的程序(含数据),成为一个能独立运行的基本单位或与其它进程并发执行的进程。或者说,OS是根据PCB来对并发执行的进程进行控制和管理的。 PCB一般是系统内存占用区中的一个连续存区,它存放着操做系统用于描述进程状况及控制进程运行所需的所有信息 

进程的阻塞

正在执行的进程,因为期待的某些事件未发生,如请求系统资源失败、等待某种操做的完成、新数据还没有到达或无新工做作等,则由系统自动执行阻塞原语(Block),使本身由运行状态变为阻塞状态。可见,进程的阻塞是进程自身的一种主动行为,也所以只有处于运行态的进程(得到CPU),才可能将其转为阻塞状态。当进程进入阻塞状态,是不占用CPU资源的

文件描述符fd

文件描述符(File descriptor)是计算机科学中的一个术语,是一个用于表述指向文件的引用的抽象化概念。

文件描述符在形式上是一个非负整数。实际上,它是一个索引值,指向内核为每个进程所维护的该进程打开文件的记录表。当程序打开一个现有文件或者建立一个新文件时,内核向进程返回一个文件描述符。在程序设计中,一些涉及底层的程序编写每每会围绕着文件描述符展开。可是文件描述符这一律念每每只适用于UNIX、Linux这样的操做系统。

缓存 I/O

缓存 I/O 又被称做标准 I/O,大多数文件系统的默认 I/O 操做都是缓存 I/O。在 Linux 的缓存 I/O 机制中,操做系统会将 I/O 的数据缓存在文件系统的页缓存( page cache )中,也就是说,数据会先被拷贝到操做系统内核的缓冲区中,而后才会从操做系统内核的缓冲区拷贝到应用程序的地址空间。

缓存 I/O 的缺点:
数据在传输过程当中须要在应用程序地址空间和内核进行屡次数据拷贝操做,这些数据拷贝操做所带来的 CPU 以及内存开销是很是大的。

IO模式

刚才说了,对于一次IO访问(以read举例),数据会先被拷贝到操做系统内核的缓冲区中,而后才会从操做系统内核的缓冲区拷贝到应用程序的地址空间。因此说,当一个read操做发生时,它会经历两个阶段:
1. 等待数据准备 (Waiting for the data to be ready)
2. 将数据从内核拷贝到进程中 (Copying the data from the kernel to the process)

正式由于这两个阶段,linux系统产生了下面五种网络模式的方案。
- 阻塞 I/O(blocking IO)
- 非阻塞 I/O(nonblocking IO)
- I/O 多路复用( IO multiplexing)
- 信号驱动 I/O( signal driven IO)
- 异步 I/O(asynchronous IO)

注:因为signal driven IO在实际中并不经常使用,因此我这只说起剩下的四种IO Model。

阻塞 I/O(blocking IO)

在linux中,默认状况下全部的socket都是blocking,一个典型的读操做流程大概是这样:

 

当用户进程调用了recvfrom这个系统调用,kernel就开始了IO的第一个阶段:准备数据(对于网络IO来讲,不少时候数据在一开始尚未到达。好比,尚未收到一个完整的UDP包。这个时候kernel就要等待足够的数据到来)。这个过程须要等待,也就是说数据被拷贝到操做系统内核的缓冲区中是须要一个过程的。而在用户进程这边,整个进程会被阻塞(固然,是进程本身选择的阻塞)。当kernel一直等到数据准备好了,它就会将数据从kernel中拷贝到用户内存,而后kernel返回结果,用户进程才解除block的状态,从新运行起来。

因此,blocking IO的特色就是在IO执行的两个阶段都被block了。

非阻塞 I/O(nonblocking IO)

linux下,能够经过设置socket使其变为non-blocking。当对一个non-blocking socket执行读操做时,流程是这个样子:

 

当用户进程发出read操做时,若是kernel中的数据尚未准备好,那么它并不会block用户进程,而是马上返回一个error。从用户进程角度讲 ,它发起一个read操做后,并不须要等待,而是立刻就获得了一个结果。用户进程判断结果是一个error时,它就知道数据尚未准备好,因而它能够再次发送read操做。一旦kernel中的数据准备好了,而且又再次收到了用户进程的system call,那么它立刻就将数据拷贝到了用户内存,而后返回。

因此,nonblocking IO的特色是用户进程须要不断的主动询问kernel数据好了没有。

I/O 多路复用( IO multiplexing)

IO multiplexing就是咱们说的select,poll,epoll,有些地方也称这种IO方式为event driven IO。select/epoll的好处就在于单个process就能够同时处理多个网络链接的IO。它的基本原理就是select,poll,epoll这个function会不断的轮询所负责的全部socket,当某个socket有数据到达了,就通知用户进程。

当用户进程调用了select,那么整个进程会被block,而同时,kernel会“监视”全部select负责的socket,当任何一个socket中的数据准备好了,select就会返回。这个时候用户进程再调用read操做,将数据从kernel拷贝到用户进程。

因此,I/O 多路复用的特色是经过一种机制一个进程能同时等待多个文件描述符,而这些文件描述符(套接字描述符)其中的任意一个进入读就绪状态,select()函数就能够返回。

这个图和blocking IO的图其实并无太大的不一样,事实上,还更差一些。由于这里须要使用两个system call (select 和 recvfrom),而blocking IO只调用了一个system call (recvfrom)。可是,用select的优点在于它能够同时处理多个connection。

因此,若是处理的链接数不是很高的话,使用select/epoll的web server不必定比使用multi-threading + blocking IO的web server性能更好,可能延迟还更大。select/epoll的优点并非对于单个链接能处理得更快,而是在于能处理更多的链接。)

在IO multiplexing Model中,实际中,对于每个socket,通常都设置成为non-blocking,可是,如上图所示,整个用户的process实际上是一直被block的。只不过process是被select这个函数block,而不是被socket IO给block。

异步 I/O(asynchronous IO)

inux下的asynchronous IO其实用得不多。先看一下它的流程:

用户进程发起read操做以后,马上就能够开始去作其它的事。而另外一方面,从kernel的角度,当它受到一个asynchronous read以后,首先它会马上返回,因此不会对用户进程产生任何block。而后,kernel会等待数据准备完成,而后将数据拷贝到用户内存,当这一切都完成以后,kernel会给用户进程发送一个signal,告诉它read操做完成了。

总结

blocking和non-blocking的区别

调用blocking IO会一直block住对应的进程直到操做完成,而non-blocking IO在kernel还准备数据的状况下会马上返回。

synchronous IO和asynchronous IO的区别

在说明synchronous IO和asynchronous IO的区别以前,须要先给出二者的定义。POSIX的定义是这样子的:
- A synchronous I/O operation causes the requesting process to be blocked until that I/O operation completes;
- An asynchronous I/O operation does not cause the requesting process to be blocked;

二者的区别就在于synchronous IO作”IO operation”的时候会将process阻塞。按照这个定义,以前所述的blocking IO,non-blocking IO,IO multiplexing都属于synchronous IO。

有人会说,non-blocking IO并无被block啊。这里有个很是“狡猾”的地方,定义中所指的”IO operation”是指真实的IO操做,就是例子中的recvfrom这个system call。non-blocking IO在执行recvfrom这个system call的时候,若是kernel的数据没有准备好,这时候不会block进程。可是,当kernel中数据准备好的时候,recvfrom会将数据从kernel拷贝到用户内存中,这个时候进程是被block了,在这段时间内,进程是被block的。

而asynchronous IO则不同,当进程发起IO 操做以后,就直接返回不再理睬了,直到kernel发送一个信号,告诉进程说IO完成。在这整个过程当中,进程彻底没有被block。

各个IO Model的比较如图所示:

经过上面的图片,能够发现non-blocking IO和asynchronous IO的区别仍是很明显的。在non-blocking IO中,虽然进程大部分时间都不会被block,可是它仍然要求进程去主动的check,而且当数据准备完成之后,也须要进程主动的再次调用recvfrom来将数据拷贝到用户内存。而asynchronous IO则彻底不一样。它就像是用户进程将整个IO操做交给了他人(kernel)完成,而后他人作完后发信号通知。在此期间,用户进程不须要去检查IO操做的状态,也不须要主动的去拷贝数据。

I/O 多路复用之select、poll、epoll详解

select,poll,epoll都是IO多路复用的机制。I/O多路复用就是经过一种机制,一个进程能够监视多个描述符,一旦某个描述符就绪(通常是读就绪或者写就绪),可以通知程序进行相应的读写操做。但select,poll,epoll本质上都是同步I/O,由于他们都须要在读写事件就绪后本身负责进行读写,也就是说这个读写过程是阻塞的,而异步I/O则无需本身负责进行读写,异步I/O的实现会负责把数据从内核拷贝到用户空间。(这里啰嗦下)

select

1
select(rlist, wlist, xlist, timeout = None )

select 函数监视的文件描述符分3类,分别是writefds、readfds、和exceptfds。调用后select函数会阻塞,直到有描述副就绪(有数据 可读、可写、或者有except),或者超时(timeout指定等待时间,若是当即返回设为null便可),函数返回。当select函数返回后,能够 经过遍历fdset,来找到就绪的描述符。

select目前几乎在全部的平台上支持,其良好跨平台支持也是它的一个优势。select的一 个缺点在于单个进程可以监视的文件描述符的数量存在最大限制,在Linux上通常为1024,能够经过修改宏定义甚至从新编译内核的方式提高这一限制,但 是这样也会形成效率的下降。

poll

1
int  poll (struct pollfd  * fds, unsigned  int  nfds,  int  timeout);

不一样与select使用三个位图来表示三个fdset的方式,poll使用一个 pollfd的指针实现。

struct pollfd { int fd; /* file descriptor */ short events; /* requested events to watch */ short revents; /* returned events witnessed */ }; 

pollfd结构包含了要监视的event和发生的event,再也不使用select“参数-值”传递的方式。同时,pollfd并无最大数量限制(可是数量过大后性能也是会降低)。 和select函数同样,poll返回后,须要轮询pollfd来获取就绪的描述符。

从上面看,select和poll都须要在返回后,经过遍历文件描述符来获取已经就绪的socket。事实上,同时链接的大量客户端在一时刻可能只有不多的处于就绪状态,所以随着监视的描述符数量的增加,其效率也会线性降低。

epoll

epoll是在2.6内核中提出的,是以前的select和poll的加强版本。相对于select和poll来讲,epoll更加灵活,没有描述符限制。epoll使用一个文件描述符管理多个描述符,将用户关系的文件描述符的事件存放到内核的一个事件表中,这样在用户空间和内核空间的copy只需一次。

一 epoll操做过程

epoll操做过程须要三个接口,分别以下:

1
2
3
int  epoll_create( int  size); //建立一个epoll的句柄,size用来告诉内核这个监听的数目一共有多大
int  epoll_ctl( int  epfd,  int  op,  int  fd,  struct  epoll_event *event);
int  epoll_wait( int  epfd,  struct  epoll_event * events,  int  maxevents,  int  timeout);

1. int epoll_create(int size);
  建立一个epoll的句柄,size用来告诉内核这个监听的数目一共有多大,这个参数不一样于select()中的第一个参数,给出最大监听的fd+1的值,参数size并非限制了epoll所能监听的描述符最大个数,只是对内核初始分配内部数据结构的一个建议
  当建立好epoll句柄后,它就会占用一个fd值,在linux下若是查看/proc/进程id/fd/,是可以看到这个fd的,因此在使用完epoll后,必须调用close()关闭,不然可能致使fd被耗尽。

2. int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event);
  函数是对指定描述符fd执行op操做。
  - epfd:是epoll_create()的返回值。
  - op:表示op操做,用三个宏来表示:添加EPOLL_CTL_ADD,删除EPOLL_CTL_DEL,修改EPOLL_CTL_MOD。分别添加、删除和修改对fd的监听事件。
  - fd:是须要监听的fd(文件描述符)
  - epoll_event:是告诉内核须要监听什么事

3. int epoll_wait(int epfd, struct epoll_event * events, int maxevents, int timeout);  等待epfd上的io事件,最多返回maxevents个事件。  参数events用来从内核获得事件的集合,maxevents告以内核这个events有多大,这个maxevents的值不能大于建立epoll_create()时的size,参数timeout是超时时间(毫秒,0会当即返回,-1将不肯定,也有说法说是永久阻塞)。该函数返回须要处理的事件数目,如返回0表示已超时。

相关文章
相关标签/搜索