Asynchronous module import for asyncio
Project description
aioimport
Asynchronous module import for asyncio
Getting Started
Installing
Install from PyPI using:
pip install aioimport
The problem
Some nasty modules have long running operations during import
The naive solution
First thing that comes to mind is to postpone import by moving it into the function that need that module:
async def my_work(self) -> None:
import this # will block until imported
It reduces your startup time, but it is still blocking your event loop.
That's where aioimport
comes in.
Usage
The preferred way to use aioimport
is:
import aioimport
async def my_work(self) -> None:
await aioimport.cache_module("this") # will asynchronously import module
import this # will be instantaneous since `this` is already in import cache
Also aioimport
can be also used the following way:
import aioimport
async def my_work(self) -> None:
this = await aioimport.import_module("this") # will asynchronously import module
# now `this` has exactly the same module as you would have gotten be doing `import this`
But your types will be all messed up.
How it works
Module import is done in thread executor.
Be aware of the fact that GIL still exists and technically import is done concurrently rather than in parallel with your code.
License
This project is licensed under the MIT License - see the LICENSE file for details
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
Built Distribution
Hashes for aioimport-0.1.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a339dfbee15f69efd6de6e5059fa95d410ff1317c2f8df7cd66762a729fd131c |
|
MD5 | d270cf54f3a444e9cb581fb866206dce |
|
BLAKE2b-256 | 4487f7bfabefae11e6a8164179a5022f9c716e5c871214341b25c20d5eabbde2 |