2018年8月26日多协程编程总结

今天遇到的新单词:
synchronous adj同步的
asynchronous  adj异步的
subscript n下标
split v分开
coroutine n协程

同步sync
异步async


同步,异步,阻塞非阻塞?
同步异步指的是消息的通信机制 (synchronous communication/ asynchronous communication)
1.所谓同步,就是调用者在发出一个调用请求时,一直处于等待状态,直到该请求得到结果
2.异步就是发出一个调用之后,不用在那一直等可以去做其他的事情,直到被调用者通过状
态来通知调用者,或通过回调函数处理这个调用。异步一般是配合非阻塞使用的,这样才
能发挥异步的效用,否则异步是没有意义的。
3.阻塞是指调用结果返回之前,当前线程会被挂起,不能去干其他事情
4.非阻塞调用指在不能立刻得到结果之前,该调用不会阻塞当前线程,当前进程可以去做其他的事情
(参考地址:https://blog.csdn.net/intelrain/article/details/80449473)
进程中同步不是同时,异步是同时,异步可以同时执行多个进程

target_socket = client_dict.get(nickname)[0]
TypeError: 'NoneType' object is not subscriptable
这种错误是用户输入的内容没有按照规定的格式

a = msg.decode("utf-8").split(":")[0]
语句的意思是用:将解码出来的结果分开并指定分开后的第一个值用a接收一下

套接字中包括的信息:
<socket.socket fd=500, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM,
 proto=0, laddr=('192.168.13.7', 61808), raddr=('192.168.13.7', 8080)>
地址家族,套接字类型,协议,本机地址和端口,远端地址和端口

多线程是依赖于硬件的,当线程的是数量超过一定的数量,处理效率会大量下降,所以有了机群

核心在于多任务

Tornado框架对于并发事件的处理效率比Django高,但是Django框架对web的开发效率快

用socket套接字进行网络编程,接收消息需要用decode解码之后才能将信息展示出来,
发送消息需要用encode编码之后才能进行发送。

*************************************************************************
协程总结:
实现多协程并发的方式有:
1.使用greenlet模块,该模块的特点是需要指定什么时间运行哪个协程,也就是
需要手动切换需要执行的协程

from greenlet import greenlet
 
def test1():
    print 12
    gr2.switch()   #切换到协程2执行指定的函数
    print 34
 
def test2():
    print 56
    gr1.switch()   #切换到协程1执行指定的函数
    print 78
 
#创建两个协程对象
gr1 = greenlet(test1)
gr2 = greenlet(test2)
gr1.switch()

输出:
12
56
34

**************************************************************

2.使用gevent模块,该模块提供了基于事件的单线程多任务事件管理机制,
该方式任务的切换是自动的。
Python通过yield提供了对协程的基本支持,但是不完全。而第三方的gevent为Python提供了比较完善的协程支持。

gevent是第三方库,通过greenlet实现协程,其基本思想是:

当一个greenlet遇到IO操作时,比如访问网络,就自动切换到其他的greenlet,等到IO操作完成,再在适当的时
候切换回来继续执行。由于IO操作非常耗时,经常使程序处于等待状态,有了gevent为我们自动切换协程,就保证
总有greenlet在运行,而不是等待IO
import gevent

def sing():
    while 1:
        print("回忆总想哭.....")
        # 切换标志:让步:可以执行一个异步任务
        gevent.sleep(1)

def dance():
    while 1:
        print("伤心总是难免的。。。")
        # 切换标志:让步
        gevent.sleep(1)

if __name__ == "__main__":
    # 创建协程序
    g1 = gevent.spawn(sing)
    g2 = gevent.spawn(dance)

    #独占执行     
    g1.join()
    g2.join()

**************************************************************

3.基于python生成器的协程并发操作,也就是Python自带的
  关键字:yield
def sing():
    while 1:
        print("唱歌>>>>>>>>")
        yield()  # 协程让步->让同一个线程中的其他协程可以执行。

def dance():
    while 1:
        print("跳舞<<<<<<<<")
        next(s)# 主动调用->通过next()函数,直接调用sing()协程开始运行

if __name__ == "__main__":
    s = sing()
    d = dance()

**************************************************************

4.python3.4提供了异步io模块: asyncio[异步非阻塞]用于创建多个协程并发执行
  协程:coroutine
import asyncio

# 声明一个协程函数
@asyncio.coroutine
def sing():
    while 1:
        print("唱吧唱吧.......")
        # 协程让步:执行异步操作,让另一个函数也同时执行
        yield from asyncio.sleep(2)


@asyncio.coroutine
def dance():
    while 1:
        print("跳舞跳舞.......")
        yield from asyncio.sleep(1)


if __name__ == "__main__":
    # 创建一个事件轮询对象
    loop = asyncio.get_event_loop()

    # 编译多个函数[事件]到轮询对象中
    loop.run_until_complete(asyncio.gather(sing(), dance()))

    # 关闭事件轮询对象
    loop.close()

**************************************************************

5.python3.5 对于异步IO的协程操作方式又有了新的改进
 可以直接定义异步处理函数,通过异步处理函数完成协程并发操作
import asyncio

async def sing():
    # 声明异步函数
    while 1:
        print("唱吧唱吧唱吧.........")
        await asyncio.sleep(1)  # 模拟这里产生了一个异步操作[异步IO]


async def dance():
    # 声明异步函数
    while 1:
        print("跳舞跳舞跳舞。。。")
        await asyncio.sleep(2)

if __name__ == "__main__":

    # 创建一个事件轮询对象
    loop = asyncio.get_event_loop()

    # 编译多个函数[事件]到轮询对象中
    loop.run_until_complete(asyncio.gather(sing(), dance()))
    
    # 关闭事件轮询对象
    loop.close()
 

猜你喜欢

转载自blog.csdn.net/qq_40994972/article/details/82078066
今日推荐