Skip to content

Instantly share code, notes, and snippets.

@netologist
Forked from Integralist/asyncio.wait.py
Created May 25, 2022 11:55
Show Gist options
  • Select an option

  • Save netologist/9e9aace8832aec2b66e5a93791e3f578 to your computer and use it in GitHub Desktop.

Select an option

Save netologist/9e9aace8832aec2b66e5a93791e3f578 to your computer and use it in GitHub Desktop.

Revisions

  1. @Integralist Integralist revised this gist Jul 5, 2018. No changes.
  2. @Integralist Integralist created this gist Nov 1, 2016.
    23 changes: 23 additions & 0 deletions asyncio.wait.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    import time
    import asyncio
    import requests

    domain = 'http://integralist.co.uk'
    a = '{}/foo?run={}'.format(domain, time.time())
    b = '{}/bar?run={}'.format(domain, time.time())

    async def get(url):
    print('start: ', url)
    r = requests.get(url)
    print('done: ', url)
    return await asyncio.sleep(0, result=r)

    async def get_pages(x, y):
    tasks = [get(x), get(y)]
    done, pending = await asyncio.wait(tasks, return_when=FIRST_COMPLETED) # also FIRST_EXCEPTION and ALL_COMPLETED (default)
    print('>> done: ', done)
    print('>> pending: ', pending) # will be empty if using default return_when setting

    loop = asyncio.get_event_loop()
    loop.run_until_complete(get_pages(a, b))
    loop.close()