async模块原始异步

# coding=utf-8


import asyncio
import aiohttp


async def myfunc(url):
    async with aiohttp.ClientSession() as session:
        payload = {'image_url': url}
        async with session.post('http://120.133.26.121:23456', json=payload) as r:
            print(r.status)
            print(await r.text())

async def consumer(q):
    print('consumer: starting')
    while True:

        print('consumer: waiting for item')
        item = await q.get()
        print('consumer: waited for item :{}'.format(item))

        if item is None:
            q.task_done()
            break
        else:
            await myfunc(item.get("url"))
            q.task_done()
    print('consumer: ending')


async def producer(q, data):
    print('producer: starting')

    if data is not None:
        for item in data:
            await q.put(item)
    else:
        await q.put(None)

    await q.join()
    print('producer: ending')


async def main(loop, num_consumers):

    q = asyncio.Queue(maxsize=num_consumers)
    consumers = [loop.create_task(consumer(q))]

    data = [
    {"url": "http://s3.xx.com/ba7571a95d64eaa69a49912f26816e2f.jpg"},
    {"url": "http://s3.xx.com/fedeaeb717e270368c13afd815d65bc1.jpg"},
    {"url": "http://s3.xx.com/fed3c734cef3f01909f2017212b88db6.jpg"},
    ]
    prod = [loop.create_task(producer(q, data))]

    await asyncio.wait(consumers + prod)


event_loop = asyncio.get_event_loop()

try:
    event_loop.run_until_complete(main(event_loop, 2))

finally:
    event_loop.close()

  

posted @ 2019-04-13 17:54  Adamanter  阅读(192)  评论(0编辑  收藏  举报