摘要:

  1. 协程
  2. Select\Poll\Epoll异步IO与事件驱动
  3. Python连接MySQL数据库操作
  4. RabbitMQ队列
  5. Redis\Memcached缓存
  6. Paramiko
  7. Twsited网络框架

1. 协程:

线程和进程的操作是由程序触发系统接口,最后的执行者是系统;而协程的操作者则是程序员。

协程存在的意义:对于多线程应用,CPU通过切片的方式来切换线程间的执行,线程切换时需要耗时(保存状态,下次继续)。协程则只使用一个线程,在一个线程中规定某个代码块执行顺序。

协程的适用场景:当程序中存在大量不需要CPU的操作时(IO),适用于协程。

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()

output==>

12
56
34
78
import gevent

def foo():
    print("\033[31;1m fooooooooooooooooooooo\033[0m")
    gevent.sleep(1)
    print("\033[32;1m back to foo\033[0m")

def bar():
    print("\033[33;1m barrrrrrrrrrrrrrrr\033[0m")
    gevent.sleep(1)
    print("\033[36;1m back to bar\033[0m")

def exx():
    print("\033[37;1m exxxxxxxxxxxxxxxxxx\033[0m")
    gevent.sleep(1)
    print("\033[38;1m back to exx\033[0m")

gevent.joinall([
    gevent.spawn(foo),
    gevent.spawn(bar),
    gevent.spawn(exx),
])

output==>

 fooooooooooooooooooooo
 barrrrrrrrrrrrrrrr
 exxxxxxxxxxxxxxxxxx
 back to foo
 back to exx
 back to bar

遇到IO操作直接自动切换任务:

import gevent
from urllib.request import urlopen
from gevent import monkey
monkey.patch_all()

def f(url):
    print("GET:", url)
    resp = urlopen(url)
    data = resp.read()
    print("{} bytes received from {}.".format(len(data), url))

gevent.joinall([
    gevent.spawn(f, "https://www.baidu.com"),
    gevent.spawn(f, "https://www.yahoo.com"),
    gevent.spawn(f, "https://www.github.com"),
])

output==>

GET: https://www.baidu.com
GET: https://www.yahoo.com
GET: https://www.github.com
227 bytes received from https://www.baidu.com.
395547 bytes received from https://www.yahoo.com.
24748 bytes received from https://www.github.com.

1.2 IO多路复用:

I/O多路复用指:通过一种机制,可以监视多个描述符,一旦某个描述符就绪(一般是读就绪或者写就绪),能够通知程序进行相应的读写操作。

① Linux中的 select,poll,epoll 都是IO多路复用的机制。

② Python中的select

#! /usr/bin/env python
# -*- coding: utf-8 -*-
# __author__ = "Q1mi"

"""
利用select实现的伪同时处理多个socket客户端请求:服务端
source:https://pymotw.com/3/select/
"""

import select
import socket
import queue
import sys

# 生成一个socket实例
server = socket.socket()
# 设置为非阻塞
server.setblocking(False)
# 配置IP和端口
ip_port = ("127.0.0.1", 4444)
# 绑定IP和端口
server.bind(ip_port)

# 监听五个连接
server.listen(5)

# 生成一个readable的列表,用于存放所有输入的信息
inputs = [server, ]

# 生成一个writeable的列表,用于存放所有输出的信息
outputs = []

# 生成一个消息队列
message_queue = {}

while inputs:
    print("\nWaiting for the next event.", file=sys.stderr)
    # select返回三个列表,readable中存放可以接收数据的连接,writeable存放可以发送数据的连接,exceptional存放发生错误的连接
    readable, writeable, exceptional = select.select(inputs, outputs, inputs)

    # 处理可以读取的连接
    for s in readable:
        # 如果server在inputs列表里,表示server已经准备好接收连接请求
        if s is server:
            # 接收连接请求
            conn, addr = s.accept()
            # 打印提示信息
            print("A new collection from:", addr, file=sys.stderr)
            # 将该连接设为非阻塞
            conn.setblocking(0)
            # 在inputs列表中放入该连接
            inputs.append(conn)
            # 为每个连接生成一个消息队列,用于存放我们要发送给该连接的数据
            message_queue[conn] = queue.Queue()
        # 不是server就是具体的连接
        else:
            # 接收数据
            data = s.recv(1024)
            # 如果数据存在
            if data:
                # 打印可能出现的错误信息
                print("Received {!r} from {}.".format(data, s.getpeername()), file=sys.stderr)
                # 将收到的数据存放到以该连接为key值的消息队列中
                message_queue[s].put(data)
                # 如果该连接不在可以输出消息的socket连接列表中
                if s not in outputs:
                    # 把该连接加入到可以输出消息的socket连接列表中
                    outputs.append(s)
            # 如果没有数据就关闭连接
            else:
                print("Closing {} ,after reading no data.".format(addr), file=sys.stderr)
                # 该连接如果存在于可以输出消息的socket连接列表中,就删除
                if s in outputs:
                    outputs.remove(s)
                # 也无需继续等待该连接的输入
                inputs.remove(s)
                # 关闭该连接
                s.close()
                # 从消息队列中删除该连接的消息
                del message_queue[s]

    # 处理输出的
    for s in writeable:
        try:
            next_msg = message_queue[s].get_nowait()
        # 如果消息队列为空
        except queue.Empty:
            # 打印提示信息
            print("Output queue for {} is empty.".format(s.getpeername()), file=sys.stderr)
            # 从可输出信息的socket连接列表中删除该连接
            outputs.remove(s)
        # 如果消息队列里有信息就将该信息发出。
        else:
            # 打印提示信息
            print("Sending {!r} to {}.".format(next_msg, s.getpeername()), file=sys.stderr)
            s.send(next_msg)
    # 处理异常情况
    for s in exceptional:
        # 打印提示信息
        print("Exceptional condition for {}.".format(s.getpeername()), file=sys.stderr)
        # 不再监听该连接是否可读
        inputs.remove(s)
        # 也不再监听是否可以对该连接发送数据
        if s in outputs:
            outputs.remove(s)
        # 关闭连接
        s.close()

        # 删除该连接的消息队列
        del message_queue[s]

利用select实现的伪同时处理多个socket客户端请求:服务端

#! /usr/bin/env python
# -*- coding: utf-8 -*-
# __author__ = "Q1mi"

"""
利用select实现的伪同时处理多个socket客户端请求:客户端
source:https://pymotw.com/3/select/
"""

import socket
import sys

# 定义一个消息列表
message_list = [
    "This is the message.",
    "It will be sent ",
    "in parts.",
]

# 服务器的IP和端口信息
server_ip_port = ("127.0.0.1", 4444)

# 生成一堆socket实例
client_list = [
    socket.socket(),
    socket.socket(),
    # socket.socket(),
    # socket.socket(),
]

print("Connecting to {} port {}.".format(*server_ip_port), file=sys.stderr)

# 将socket连接实例分别去连接server端
for c in client_list:
    c.connect(server_ip_port)

# 将消息列表中的信息循环发送给server端
for message in message_list:
    outgoing_data = message.encode()
    for c in client_list:
        print("{}: sending {!r}.".format(c.getsockname(), outgoing_data), file=sys.stderr)
        c.send(outgoing_data)

    # 同时也接收server端返回的数据
    for c in client_list:
        data = c.recv(1024)
        print("{}: received {!r}.".format(c.getsockname(), data), file=sys.stderr)
        # 如果数据为空就关闭该连接
        if not data:
            print("Closing socket:{}".format(c.getsockname()), file=sys.stderr)
            c.close()

利用select实现的伪同时处理多个socket客户端请求:客户端

server端:
Waiting for the next event.
A new collection from: ('127.0.0.1', 12228)

Waiting for the next event.
A new collection from: ('127.0.0.1', 12229)
Received b'This is the message.' from ('127.0.0.1', 12228).

Waiting for the next event.
Received b'This is the message.' from ('127.0.0.1', 12229).
Sending b'This is the message.' to ('127.0.0.1', 12228).

Waiting for the next event.
Output queue for ('127.0.0.1', 12228) is empty.
Sending b'This is the message.' to ('127.0.0.1', 12229).

Waiting for the next event.
Output queue for ('127.0.0.1', 12229) is empty.

Waiting for the next event.
Received b'It will be sent ' from ('127.0.0.1', 12228).

Waiting for the next event.
Received b'It will be sent ' from ('127.0.0.1', 12229).
Sending b'It will be sent ' to ('127.0.0.1', 12228).

Waiting for the next event.
Output queue for ('127.0.0.1', 12228) is empty.
Sending b'It will be sent ' to ('127.0.0.1', 12229).

Waiting for the next event.
Output queue for ('127.0.0.1', 12229) is empty.

Waiting for the next event.
Received b'in parts.' from ('127.0.0.1', 12228).

Waiting for the next event.
Received b'in parts.' from ('127.0.0.1', 12229).
Sending b'in parts.' to ('127.0.0.1', 12228).

Waiting for the next event.
Output queue for ('127.0.0.1', 12228) is empty.
Sending b'in parts.' to ('127.0.0.1', 12229).

Waiting for the next event.
Output queue for ('127.0.0.1', 12229) is empty.

Waiting for the next event.
Closing ('127.0.0.1', 12229) ,after reading no data.

Waiting for the next event.
Closing ('127.0.0.1', 12229) ,after reading no data.

Waiting for the next event.
...
==============================================
client端:
Connecting to 127.0.0.1 port 4444.
('127.0.0.1', 12228): sending b'This is the message.'.
('127.0.0.1', 12229): sending b'This is the message.'.
('127.0.0.1', 12228): received b'This is the message.'.
('127.0.0.1', 12229): received b'This is the message.'.
('127.0.0.1', 12228): sending b'It will be sent '.
('127.0.0.1', 12229): sending b'It will be sent '.
('127.0.0.1', 12228): received b'It will be sent '.
('127.0.0.1', 12229): received b'It will be sent '.
('127.0.0.1', 12228): sending b'in parts.'.
('127.0.0.1', 12229): sending b'in parts.'.
('127.0.0.1', 12228): received b'in parts.'.
('127.0.0.1', 12229): received b'in parts.'.

结果

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

3.Python连接MySQL数据库操作

4.RabbitMQ

5.Redis\Memcached缓存

6.Paramiko

7.Twsited网络框架

Python之路Day9的更多相关文章

  1. Python之路,Day9 - 异步IO\数据库\队列\缓存

    https://www.cnblogs.com/alex3714/articles/5248247.html http://www.cnblogs.com/wupeiqi/articles/51327 ...

  2. Python之路,Day9, 进程、线程、协程篇

    本节内容 操作系统发展史介绍 进程.与线程区别 python GIL全局解释器锁 线程 语法 join 线程锁之Lock\Rlock\信号量 将线程变为守护进程 Event事件 queue队列 生产者 ...

  3. Python之路,Day9 - 线程、进程、协程和IO多路复用

    参考博客: 线程.进程.协程: http://www.cnblogs.com/wupeiqi/articles/5040827.html http://www.cnblogs.com/alex3714 ...

  4. Python之路,Day9 , IO多路复用(番外篇)

    同步IO和异步IO,阻塞IO和非阻塞IO分别是什么,到底有什么区别?不同的人在不同的上下文下给出的答案是不同的.所以先限定一下本文的上下文. 本文讨论的背景是Linux环境下的network IO. ...

  5. Python之路,Day10 - 异步IO\数据库\队列\缓存

    Python之路,Day9 - 异步IO\数据库\队列\缓存   本节内容 Gevent协程 Select\Poll\Epoll异步IO与事件驱动 Python连接Mysql数据库操作 RabbitM ...

  6. Python之路【第一篇】python基础

    一.python开发 1.开发: 1)高级语言:python .Java .PHP. C#  Go ruby  c++  ===>字节码 2)低级语言:c .汇编 2.语言之间的对比: 1)py ...

  7. Python之路

    Python学习之路 第一天   Python之路,Day1 - Python基础1介绍.基本语法.流程控制              第一天作业第二天   Python之路,Day2 - Pytho ...

  8. python之路 目录

    目录 python python_基础总结1 python由来 字符编码 注释 pyc文件 python变量 导入模块 获取用户输入 流程控制if while python 基础2 编码转换 pych ...

  9. Python之路【第十九篇】:爬虫

    Python之路[第十九篇]:爬虫   网络爬虫(又被称为网页蜘蛛,网络机器人,在FOAF社区中间,更经常的称为网页追逐者),是一种按照一定的规则,自动地抓取万维网信息的程序或者脚本.另外一些不常使用 ...

随机推荐

  1. C#中小函数的应用

    今天看到的一段函数 StringBuilder sb = new StringBuilder("sselect * from table where 1=1"); if(TextB ...

  2. 基于FPGA的key button等开关消抖,按键消抖电路设计

    最近要用上一个key消抖的功能.于是找到了之前写的并放入博客的程序,发现居然全部有问题.http://www.cnblogs.com/sepeng/p/3477215.html —— 有问题,包括很多 ...

  3. 64位系统未注册"MSDAORA.1"提供程序

    原因:如错误,64位系统未注册"MSDAORA.1"提供程序 解决:在IIS应用程序池中找到自己的网站,打开高级设置,设置“启用32位应用程序”为“True”即可. 另外还有其他解 ...

  4. LintCode-比较字符串

    题目描述: 比较两个字符串A和B,确定A中是否包含B中所有的字符.字符串A和B中的字符都是 大写字母 注意事项 在 A 中出现的 B 字符串里的字符不需要连续或者有序. 样例 给出 A = " ...

  5. The process "E:\Qt\4.8.5\bin\qmake.exe" exited with code 2.(不能包含中文路径,qmake够弱智的)

    打开某个项目的时候,编译出现类似的错误 21:46:44: The process "E:\Qt\4.8.5\bin\qmake.exe" exited with code 2. ...

  6. maven 添加自己的包

    mvn install:install-file -Dfile=d:/flea.jar -DgroupId=com.flea.bussiness -DartifactId=flea -Dversion ...

  7. Python学习入门基础教程(learning Python)--2.2 Python下的变量基础

    变量的基本概念,变量可以这样去理解,变量是一个值,这个值存储在计算机的内存里.以网购为例,您在选购傻商品的时候,是在不同页面里选不同的商品,选好一件点击“放入购物车”,选完了再点击去结帐,这些商品的价 ...

  8. HDU 1711 Number Sequence KMP

    题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=1711 AC代码: #include <iostream> #include <cs ...

  9. HTML5+CSS3正式开课

    HTML5+CSS3 HTML5+CSS3 HTML5+CSS3

  10. 纯CSS写九宫格样式,高宽自适应正方形

    <!DOCTYPE html><html>    <head>        <meta charset="utf-8">      ...