GithubHelp home page GithubHelp logo

asyncinject's Introduction

asyncinject's People

Contributors

adriangb avatar simonw avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

asyncinject's Issues

Ability to resolve an unregistered function

I'd like to be able to do the following:

async def one():
    return 1

async def two():
    return 2

registry = Registry(one, two)

async def three(one, two):
    return one + two

result = await registry.resolve(three)

Note that three has not been registered with the registry - but it still has its parameters inspected and used to resolve the dependencies.

This would be useful for Datasette, where I want plugins to be able to interact with predefined registries without needing to worry about picking a name for their function that doesn't clash with a name that has been registered by another plugin.

Investigate a non-class-based version

I'm thinking about using this with Datasette plugins, which aren't well suited to the current class-based mechanism because plugins may want to register their own additional dependency injection functions.

A way to turn off parallel execution (for easier comparison)

Would be neat if you could toggle the parallel execution on and off, to better demonstrate the performance difference that it implements.

Would happen in this code that calls gather():

awaitables = [
instance._registry[name](
instance,
_results=results,
**{k: v for k, v in results.items() if k in instance._graph[name]},
)
for name in awaitable_names
]
awaitable_results = await asyncio.gather(*awaitables)
results.update(dict(zip(awaitable_names, awaitable_results)))

Debug mechanism

Add a mechanism which shows exactly how the class is executing, including which methods are running in parallel. Maybe even with a very basic ASCII visualization? Then use it to help illustrate the examples in the README, refs #4.

Option to pass a dictionary with explicit names

I wanted to swap out the implementation of one of the functions based on other logic (to implement _shape= support in Datasette) - but asyncinject currently uses fn.__name__ introspection, which makes this hard to do.

Would be useful if you could optionally do this instead:

registry = Registry.from_dict({
    "both": both,
    "one": one,
    "two": two,
)

Using .from_dict() here so as not to have the Registry() constructor behave differently for different types of input.

Concurrency is not being optimized

It looks like concurrency / parallelism is not being maximized due to the grouping of dependencies into node groups. Here's a simple example:

import asyncio
from time import time
from typing import Annotated

async def a():
    await asyncio.sleep(1)

async def b():
    await asyncio.sleep(2)

async def c(a):
    await asyncio.sleep(1)

async def d(b, c):
    pass

async def main_asyncinjector():
    reg = Registry(a, b, c, d)
    start = time()
    await reg.resolve(d)
    print(time()-start)

asyncio.run(main_asyncinjector())

This should take 2 seconds to run (start a and b, once a finishes start c, b and c finish at the same time and you're done) but takes 3 seconds (start a and b, wait for both to finish then start c).

This happens because graphlib.TopologicalSorter is not used online and instead it is being used to statically compute groups of dependencies.

I don't think it would be too hard to address this, but I'm not sure how much you'd want to change to accommodate this.
I work on a similar project (https://github.com/adriangb/di) and there I found it very useful to break out the concept of an "executor" out of the container/registry concept, which means that instead of a parallel option you'd have pluggable executors that could choose to use concurrency, limit concurrency, use threads instead, etc.
FWIW here's what that looks like with this example:

import asyncio
from time import time
from typing import Annotated

from asyncinject import Registry
from di.dependant import Marker, Dependant
from di.container import Container
from di.executors import ConcurrentAsyncExecutor


async def a():
    await asyncio.sleep(1)

async def b():
    await asyncio.sleep(2)

async def c(a: Annotated[None, Marker(a)]):
    await asyncio.sleep(1)

async def d(b: Annotated[None, Marker(b)], c: Annotated[None, Marker(c)]):
    pass

async def main_asyncinjector():
    reg = Registry(a, b, c, d)
    start = time()
    await reg.resolve(d)
    print(time()-start)


async def main_di():
    container = Container()
    solved = container.solve(Dependant(d), scopes=[None])
    executor = ConcurrentAsyncExecutor()
    async with container.enter_scope(None) as state:
        start = time()
        await container.execute_async(solved, executor, state=state)
        print(time()-start)

asyncio.run(main_asyncinjector())  # 3 seconds
asyncio.run(main_di())  # 2 seconds

Handle non-async functions too

It may be useful if this could also handle regular def ... functions, in addition to async def functions.

I'm imagining using this for Datasette extras where some extras might be able to operate directly on data that has already been fetched by other functions - e.g. an extra which transforms objects in some way.

Refs:

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.