• 异步非阻塞--高性能相关


    【异步】:回调

          非组塞,完成后某个任务后,自动执行一个这个函数,回调告知结果。

    【非阻塞】:不等,某一个socket,先链接,发送数据,再接收数据一气呵成,不等。

                       阻塞:client = socket();client.connet(ip,端口)

            非阻塞:client = socket();client.setblocking(False);client.connet(ip,端口)。

              链接,发送数据,接收数据。有数据拿数据,没数据直接报错。

    很多模块支持异步非阻塞

     1 import asyncio  #不常用,原理
     2 
     3 
     4 @asyncio.coroutine
     5 def func1():
     6     print('before...func1......')
     7     yield from asyncio.sleep(5)
     8     print('end...func1......')
     9 
    10 
    11 tasks = [func1(), func1()]
    12 
    13 loop = asyncio.get_event_loop()
    14 loop.run_until_complete(asyncio.gather(*tasks))
    15 loop.close()
    1.asyncio示例1
    import asyncio
    
    
    @asyncio.coroutine
    def fetch_async(host, url='/'):
        print(host, url)
        reader, writer = yield from asyncio.open_connection(host, 80)
    
        request_header_content = """GET %s HTTP/1.0
    Host: %s
    
    """ % (url, host,)
        request_header_content = bytes(request_header_content, encoding='utf-8')
    
        writer.write(request_header_content)
        yield from writer.drain()
        text = yield from reader.read()
        print(host, url, text)
        writer.close()
    
    tasks = [
        fetch_async('www.cnblogs.com', '/catherine007/'),
        fetch_async('dig.chouti.com', '/pic/show?nid=4073644713430508&lid=10273091')
    ]
    
    loop = asyncio.get_event_loop()
    results = loop.run_until_complete(asyncio.gather(*tasks))
    loop.close()
    
    1.asyncio示例2
    1.asyncio示例2
    import aiohttp
    import asyncio
    
    
    @asyncio.coroutine
    def fetch_async(url):
        print(url)
        response = yield from aiohttp.request('GET', url)
        # data = yield from response.read()
        # print(url, data)
        print(url, response)
        response.close()
    
    
    tasks = [fetch_async('http://www.google.com/'), fetch_async('http://www.chouti.com/')]
    
    event_loop = asyncio.get_event_loop()
    results = event_loop.run_until_complete(asyncio.gather(*tasks))
    event_loop.close()
    
    2.asyncio + aiohttp
    2.asyncio + aiohttp
    import asyncio
    import requests
    
    
    @asyncio.coroutine
    def fetch_async(func, *args):
        loop = asyncio.get_event_loop()
        future = loop.run_in_executor(None, func, *args)
        response = yield from future
        print(response.url, response.content)
    
    
    tasks = [
        fetch_async(requests.get, 'http://www.cnblogs.com/'),
        fetch_async(requests.get, 'http://dig.chouti.com/pic/show?nid=4073644713430508&lid=10273091')
    ]
    
    loop = asyncio.get_event_loop()
    results = loop.run_until_complete(asyncio.gather(*tasks))
    loop.close()
    
    3.asyncio + requests
    3.asyncio + requests

     gevent --Gevent依赖libevent和greenlet,需要分别安装.

          (greenlet,libevent真正实现协程的模块)

                    greenlet 1,不智能,遇到io阻塞请求不能自动切换,

               2,跟异步非阻塞没有任何关系

    协程和异步非阻塞的区别?

    没有任何关系,协程来回完成代码的切换,单纯没有意义,人为给的这个概念。

     gevent和greenlet结合起来,能完成一个异步非阻塞的模块功能。

    import gevent

    monkey.patch_all()

    #将socket 替换成setblocking(False)

    import gevent
    
    import requests
    from gevent import monkey
    
    monkey.patch_all()
    
    
    def fetch_async(method, url, req_kwargs):
        print(method, url, req_kwargs)
        response = requests.request(method=method, url=url, **req_kwargs)
        print(response.url, response.content)
    
    # ##### 发送请求 #####
    gevent.joinall([
        gevent.spawn(fetch_async, method='get', url='https://www.python.org/', req_kwargs={}),
        gevent.spawn(fetch_async, method='get', url='https://www.yahoo.com/', req_kwargs={}),
        gevent.spawn(fetch_async, method='get', url='https://github.com/', req_kwargs={}),
    ])
    
    # ##### 发送请求(协程池控制最大协程数量) #####
    # from gevent.pool import Pool
    # pool = Pool(None)
    # gevent.joinall([
    #     pool.spawn(fetch_async, method='get', url='https://www.python.org/', req_kwargs={}),
    #     pool.spawn(fetch_async, method='get', url='https://www.yahoo.com/', req_kwargs={}),
    #     pool.spawn(fetch_async, method='get', url='https://www.github.com/', req_kwargs={}),
    # ])
    
    4.gevent + requests
    4.gevent + requests
    import grequests
    
    
    request_list = [
        grequests.get('http://httpbin.org/delay/1', timeout=0.001),
        grequests.get('http://fakedomain/'),
        grequests.get('http://httpbin.org/status/500')
    ]
    
    
    # ##### 执行并获取响应列表 #####
    # response_list = grequests.map(request_list)
    # print(response_list)
    
    
    # ##### 执行并获取响应列表(处理异常) #####
    # def exception_handler(request, exception):
    # print(request,exception)
    #     print("Request failed")
    
    # response_list = grequests.map(request_list, exception_handler=exception_handler)
    # print(response_list)
    
    5.grequests
    5.grequests
    from twisted.web.client import getPage, defer
    from twisted.internet import reactor
    
    
    def all_done(arg):
        reactor.stop()
    
    
    def callback(contents):
        print(contents)
    
    
    deferred_list = []
    
    url_list = ['http://www.bing.com', 'http://www.baidu.com', ]
    for url in url_list:
        deferred = getPage(bytes(url, encoding='utf8'))
        deferred.addCallback(callback)
        deferred_list.append(deferred)
    
    dlist = defer.DeferredList(deferred_list)
    dlist.addBoth(all_done)
    
    reactor.run()
    
    6.Twisted示例
    6.Twisted示例
    from tornado.httpclient import AsyncHTTPClient
    from tornado.httpclient import HTTPRequest
    from tornado import ioloop
    
    
    def handle_response(response):
        """
        处理返回值内容(需要维护计数器,来停止IO循环),调用 ioloop.IOLoop.current().stop()
        :param response: 
        :return: 
        """
        if response.error:
            print("Error:", response.error)
        else:
            print(response.body)
    
    
    def func():
        url_list = [
            'http://www.baidu.com',
            'http://www.bing.com',
        ]
        for url in url_list:
            print(url)
            http_client = AsyncHTTPClient()
            http_client.fetch(HTTPRequest(url), handle_response)
    
    
    ioloop.IOLoop.current().add_callback(func)
    ioloop.IOLoop.current().start()
    
    7.Tornado
    7.Tornado
    from twisted.internet import reactor
    from twisted.web.client import getPage
    import urllib.parse
    
    
    def one_done(arg):
        print(arg)
        reactor.stop()
    
    post_data = urllib.parse.urlencode({'check_data': 'adf'})
    post_data = bytes(post_data, encoding='utf8')
    headers = {b'Content-Type': b'application/x-www-form-urlencoded'}
    response = getPage(bytes('http://dig.chouti.com/login', encoding='utf8'),
                       method=bytes('POST', encoding='utf8'),
                       postdata=post_data,
                       cookies={},
                       headers=headers)
    response.addBoth(one_done)
    
    reactor.run()
    
    Twisted更多
    Twisted更多

     高性能相关

    1,串行

    2,线程,进程(可完成并发,但是资源消耗多)

    3,异步非阻塞

      -asycio

      -gevent

      -twisted

      -tornado

  • 相关阅读:
    iOS 十六进制字符串 "#FFFF00" 转换成颜色对象
    iOS toast 的连续显示
    文件管理
    pod 常用命令
    键盘事件
    iOS 添加阴影
    渐变色
    Ubuntu安装flameshot截图工具
    Ubuntu安装酸酸乳客户端
    Ubuntu安装网易云音乐
  • 原文地址:https://www.cnblogs.com/catherine007/p/8608503.html
Copyright © 2020-2023  润新知