mamba-org / mamba

The Fast Cross-Platform Package Manager
https://mamba.readthedocs.io
BSD 3-Clause "New" or "Revised" License
6.92k stars 357 forks source link

Async Python API #1919

Open davidbrochart opened 2 years ago

davidbrochart commented 2 years ago

The high-level Python API has e.g. an install function, which can take quite a lot of time. On the other hand, it's doing a lot of IO to download files. If the installation is done in threads at the C++ level, then it could also leave the Python interpreter free do to other things. I'm wondering if we could have an async API, so that we can e.g. await install()?

davidbrochart commented 2 years ago

Note that this can be done outside of the mamba library, by running in a thread, for instance using anyio's to_thread:

import asyncio
from anyio import to_thread
from mamba.api import create

async def main():
    await to_thread.run_sync(create, "my_env", ("attrs", ), ("conda-forge", ))

asyncio.run(main())

But I think it makes sense to have it in mamba, because it could handle parallel installations. I guess things could go wrong with this example code if several tasks run concurrently?