Giter Site home page Giter Site logo

aioexec's Introduction

aioexec

Description

Aioexec is a simple, intuitive interface around the concurrent.futures package and asyncio's loop.run_in_executor method. Aioexec is leightweight, no dependencies and ~70 LOC.

Requirements

aioexec requires Python >= 3.6

Install

pip install aioexec

or

pipenv install aioexec

Usage

Without aioexec you usually run an executor something like this:

import aysncio
from concurrent.futures import ProcessPoolExecutor

# ...

loop = asyncio.get_event_loop()

foo = await loop.run_in_executor(
    ProcessPoolExecutor(1), lambda: my_func(foo='baz')
)

With aioexec you would do the same like this:

import aysncio
from aioexec import Procs

# ...

foo = await Procs(1).call(my_func, foo='baz') 

You can call a batch of functions in the same executor like this:

import asyncio as aio
from aioexec import Procs, Call

my_values = await aio.gather(
    *Procs(3).batch(
        Call(my_func, foo='1'),
        Call(my_func, foo='2'),
        Call(my_func, foo='3'),
    )

This plays nicely with comprehensions:

my_values = await aio.gather(
    *Procs(10).batch(
        Call(my_func, foo=i) for i in range(0, 10)
    )

It works the same for Threads:

import asyncio as aio
from aioexec import Threads

foo = await Threads(1).call(my_func, foo='baz') 

If necessary, you can pass an event loop to the executors like this:

foo = await Threads(1, my_loop).call(my_func, foo='baz') 
foo = await Procs(1, my_loop).call(my_func, foo='baz') 

Development

Just clone the repo and run:

pipenv install --dev

aioexec's People

Contributors

feluxe avatar

Watchers

Oscar Esteban avatar  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.