zoukankan      html  css  js  c++  java
  • 协程 + asyncio

    线程池

    from multiprocessing.dummy import Pool
    pool = Pool(num)   # num 开启的线程数
    pool.map(backfunc,iterable)  # 可迭代对象 仅且只有一个参数传如 回调函数内

    单线程+异步协程 (requests模块是一个非异步的模块, 支持异步的网络请求模块:aiohttp)

    事件监视器  内放 任务  task  内放 协程 
    event_loop:事件监听器 一直循环监听, 内放一些函数 用来调用    
    # loop = asyncio.get_event_loop()   创建事件循环对象
    #  loop.run_until_complete(c)  将协程注册到事件循环对象  启动事件循环
    coroutine:协程  async 关键字定义 + 函数  这个方法在调用时不会立即被执行,而是返回一个协程对象 
    #   async def func():pass
    #   c = func()
    task:任务,它是对协程对象的进一步封装,包含了任务的各个状态 
        #  task = loop.create_task(c)  对协程对象进行 进一步封装
       #  loop.run_until_complete(task)  将任务注册 并启动
    future:代表将来执行或还没有执行的任务,实际上和 task 没有本质区别。
        #  task = asyncio.ensure_future(c)
        #  loop.run_until_complete(task)

    绑定回调(task)

    def callback(task):
        print('i am callback:',task.result())
    import asyncio
    async def hello(name):
        print('hello to :',name)
        return name
    c = hello('bobo')
    task = asyncio.ensure_future(c)   
    task.add_done_callback(callback)  #给任务对象绑定一个回调函数
    loop.run_until_complete(task)

    多任务异步协程

    import asyncio
    async def request(url):
        print('正在下载:',url)
        ####  sleep(2)          #非异步模块的代码:在此处如果存在非异步操作代码,则会彻底让asyncio失去异步的效果
        await asyncio.sleep(2)
        print('下载成功:',url)
    urls = [
        'www.baidu.com',
        'www.taobao.com',
        'www.sogou.com'
    ]
    start = time.time()
    loop = asyncio.get_event_loop()
    tasks = []             #任务列表,放置多个任务对象
    for url in urls:
        c = request(url)
        task = asyncio.ensure_future(c)
        tasks.append(task)
       
    #将多个任务对象对应的列表注册到事件循环中
    loop.run_until_complete(asyncio.wait(tasks))
    print('总耗时:',time.time()-start)

    aiohttp 异步请求模块

    import aiohttp
    import asyncio
     
    async def get_page(url):
        async with aiohttp.ClientSession() as session:
            async with await session.get(url=url) as response:
                page_text = await response.text() #read()  json()
                print(page_text)
    start = time.time()
    urls = [
        'http://127.0.0.1:5000/bobo',
        'http://127.0.0.1:5000/jay',
        'http://127.0.0.1:5000/tom',
        'http://127.0.0.1:5000/bobo',
        'http://127.0.0.1:5000/jay',
        'http://127.0.0.1:5000/tom',
        'http://127.0.0.1:5000/bobo',
        'http://127.0.0.1:5000/jay',
        'http://127.0.0.1:5000/tom'
    ]
    tasks = []
    loop = asyncio.get_event_loop()
    for url in urls:
        c = get_page(url)
        task = asyncio.ensure_future(c)
        tasks.append(task)
    loop.run_until_complete(asyncio.wait(tasks))
    print('总耗时:',time.time()-start)

  • 相关阅读:
    使用 JSON JavaScriptSerializer 进行序列化或反序列化时出错。字符串的长度超过了为 maxJsonLength 属性设置的值。
    PowerDesigner 连接数据库,更新数据库;
    Spark Worker启动Driver和Executor工作流程
    获取spark-submit --files的文件内容
    JVM虚拟机选项:Xms Xmx PermSize MaxPermSize区别(转)
    在Java应用中通过SparkLauncher启动Spark任务
    通过thriftserver的beeline/jdbc等方式连接到SparkSQL
    Spark:java.net.BindException: Address already in use: Service 'SparkUI' failed after 16 retries!
    Spark Sql之ThriftServer和Beeline的使用
    Spark SQL读取Oracle的number类型的数据时精度丢失问题
  • 原文地址:https://www.cnblogs.com/qizt/p/10821036.html
Copyright © 2011-2022 走看看