Giter VIP home page Giter VIP logo

wx1798 / asyncio-executor Goto Github PK

View Code? Open in Web Editor NEW

This project forked from yutiansut/asyncio-executor

0.0 1.0 0.0 217 KB

协程执行器,起一个额外的线程执行事件循环,主线程则管理这个事件循环线程, 这个执行器不要用在协程中.

Home Page: https://python-tools.github.io/asyncio-executor/

License: MIT License

Python 43.19% Makefile 29.94% Batchfile 26.87%

asyncio-executor's Introduction

asyncio-executor

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+

asyncio-executor's People

Contributors

hsz1273327 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.