Skip to main content

asyncio-executor

Project description

Desc

Asyncio executor for running coroutines. This code is from <https://gist.github.com/seglberg/0b4487b57b4fd425c56ad72aba9971be>

keywords:asyncio,executor

Feature

  • run coroutines asynchronously

Example

  • run coroutines by using submit

from concurrent.futures import as_completed
import aiohttp
from asyncio_executor import AsyncioExecutor

async def httpget(url):
    async with aiohttp.ClientSession() as session:
        async with session.get(url) as resp:
            html = await resp.text("utf-8")
    return len(html)

with AsyncioExecutor() as executor:
    to_do = []
    urls = ["https://github.com/","https://docs.aiohttp.org/"]
    for i in urls:
        job = executor.submit(httpget,i)
        to_do.append(job)

    for future in as_completed(to_do):
        res = future.result()
  • run coroutines by using map

from concurrent.futures import as_completed
import aiohttp
from asyncio_executor import AsyncioExecutor

async def httpget(url):
    async with aiohttp.ClientSession() as session:
        async with session.get(url) as resp:
            html = await resp.text("utf-8")
    return len(html)

with AsyncioExecutor() as executor:
    result = []
    urls = ["https://github.com/", "https://docs.aiohttp.org/"]
    for i in executor.map(httpget, urls):
        result.append(i)
  • run functions by using submit

from concurrent.futures import as_completed
import requests as rq
from asyncio_executor import AsyncioExecutor

def httpsync(url):
    req = rq.get(url)
    return len(req.text)

with AsyncioExecutor() as executor:
    to_do = []
    urls = ["https://github.com/", "https://docs.aiohttp.org/"]
    for i in urls:
        job = executor.submit(httpsync, i)
        to_do.append(job)

    for future in as_completed(to_do):
        res = future.result()
        print(res)
  • run functions by using map

from concurrent.futures import as_completed
import requests as rq
from asyncio_executor import AsyncioExecutor

def httpsync(url):
    req = rq.get(url)
    return len(req.text)

with AsyncioExecutor() as executor:

    result = []
    urls = ["https://github.com/", "https://docs.aiohttp.org/"]
    for i in executor.map(httpsync, urls):
        result.append(i)
print(result)

Install

  • python -m pip install asyncio-executor

Limitations

  • only support python 3.5+

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

asyncio-executor-0.0.4.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

asyncio_executor-0.0.4-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file asyncio-executor-0.0.4.tar.gz.

File metadata

  • Download URL: asyncio-executor-0.0.4.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Python-urllib/3.6

File hashes

Hashes for asyncio-executor-0.0.4.tar.gz
Algorithm Hash digest
SHA256 b0aa050596f51d653a18bdc7a7347b4471607bd098948630eb27190fe2931b38
MD5 d40a1bbdcd9a600c8fcb581e376bbf42
BLAKE2b-256 822a66a186e00d27e5d36a5c1f308619169f9659b1957d89f6a9f2146dfc68f3

See more details on using hashes here.

File details

Details for the file asyncio_executor-0.0.4-py3-none-any.whl.

File metadata

File hashes

Hashes for asyncio_executor-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 893db7b027172450256d3336833101a873269abc889aba819b761a0d4ab8cbb0
MD5 8faadcc5aecc073a7137ea2b023c884a
BLAKE2b-256 af34c8d2a3dea646fc6b4f764d4cb46f69a9992e3cd501ce26d4ce3137937ccb

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page