├── .gitignore ├── LICENSE.txt ├── MANIFEST ├── MANIFEST.in ├── README.md ├── aioify ├── __init__.py └── __init__.pyi ├── pyproject.toml └── setup.cfg /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | .python-version 3 | __pycache__/ 4 | aioify.egg-info/ 5 | dist 6 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright 2018 Yifei Kong 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 8 | -------------------------------------------------------------------------------- /MANIFEST: -------------------------------------------------------------------------------- 1 | # file GENERATED by distutils, do NOT edit 2 | setup.cfg 3 | setup.py 4 | aioify/__init__.py 5 | -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | include *.md 2 | include LICENSE.txt 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | aioify (maintenance mode) 2 | ========================= 3 | 4 | Authors of aioify and module-wrapper decided to discontinue support of 5 | these libraries since the idea: "let's convert sync libraries to async 6 | ones" works only for some cases. Existing releases of libraries won't 7 | be removed, but don't expect any changes since today. Feel free to 8 | fork these libraries, however, we don't recommend using the automatic 9 | sync-to-async library conversion approach, as unreliable. Instead, 10 | it's better to run synchronous functions asynchronously using 11 | https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.loop.run_in_executor 12 | or https://anyio.readthedocs.io/en/stable/api.html#running-code-in-worker-threads. 13 | 14 | Old documentation 15 | ----------------- 16 | 17 | Make every function async and await-able. 18 | 19 | Usage 20 | ----- 21 | 22 | ``` 23 | pip install aioify 24 | ``` 25 | 26 | For example, make `os`, `shutil` and user defined function await-able. 27 | 28 | ```Python 29 | #!/usr/bin/env python 30 | ########### 31 | # Warning # 32 | ########### 33 | # This code should be executed only on POSIX OS with at least 1 GiB free space in /tmp/ directory and RAM! 34 | 35 | from aioify import aioify 36 | import os 37 | import shutil 38 | 39 | 40 | def generate_big_file(filename, file_size): 41 | with open(file=filename, mode='wb') as f: 42 | f.write(os.urandom(file_size)) 43 | 44 | 45 | aiogenerate_big_file = aioify(obj=generate_big_file) 46 | aios = aioify(obj=os, name='aios') 47 | aioshutil = aioify(obj=shutil, name='aishutil') 48 | 49 | 50 | async def main(): 51 | dir_path = '/tmp/big-files/' 52 | await aios.makedirs(name=dir_path, exist_ok=True) 53 | filename = os.path.join(dir_path, 'original') 54 | copy_filename = os.path.join(dir_path, 'copy') 55 | file_size = 1024 * 1024 * 1024 56 | await aiogenerate_big_file(filename=filename, file_size=file_size) 57 | await aioshutil.copy(src=filename, dst=copy_filename) 58 | await aioshutil.rmtree(path=dir_path) 59 | 60 | 61 | if __name__ == '__main__': 62 | import asyncio as aio 63 | loop = aio.get_event_loop() 64 | loop.run_until_complete(main()) 65 | ``` 66 | -------------------------------------------------------------------------------- /aioify/__init__.py: -------------------------------------------------------------------------------- 1 | from functools import wraps, partial 2 | import asyncio 3 | import functools 4 | import inspect 5 | 6 | import module_wrapper 7 | 8 | 9 | __all__ = ['aioify'] 10 | wrapper_assignments = tuple(x for x in functools.WRAPPER_ASSIGNMENTS if x != '__annotations__') 11 | 12 | 13 | def wrap(func): 14 | @wraps(func, assigned=wrapper_assignments) 15 | async def run(*args, loop=None, executor=None, **kwargs): 16 | if loop is None: 17 | loop = asyncio.get_event_loop() 18 | pfunc = partial(func, *args, **kwargs) 19 | return await loop.run_in_executor(executor, pfunc) 20 | 21 | @wraps(func, assigned=wrapper_assignments) 22 | async def coroutine_run(*args, **kwargs): 23 | _, _ = args, kwargs 24 | return await func 25 | 26 | @wraps(func, assigned=wrapper_assignments) 27 | async def coroutine_function_run(*args, **kwargs): 28 | return await func(*args, **kwargs) 29 | 30 | if inspect.iscoroutine(func): 31 | result = coroutine_run 32 | elif inspect.iscoroutinefunction(func): 33 | result = coroutine_function_run 34 | else: 35 | result = run 36 | return result 37 | 38 | 39 | def default_create_name_function(cls): 40 | _ = cls 41 | return 'create' 42 | 43 | 44 | def aioify(obj, name=None, create_name_function=None, skip=(), wrap_return_values=False, wrapping_scope_regex=None): 45 | create_name_function = create_name_function or default_create_name_function 46 | 47 | def create(cls): 48 | func = wrap(func=cls) if inspect.isclass(object=cls) else None 49 | return create_name_function(cls=cls), func 50 | 51 | return module_wrapper.wrap( 52 | obj=obj, 53 | wrapper=wrap, 54 | methods_to_add={create}, 55 | name=name, 56 | skip=skip, 57 | wrap_return_values=wrap_return_values, 58 | wrapping_scope_regex=wrapping_scope_regex, 59 | ) 60 | -------------------------------------------------------------------------------- /aioify/__init__.pyi: -------------------------------------------------------------------------------- 1 | from typing import Any, Callable, Collection 2 | 3 | 4 | def aioify(obj: Any, 5 | name: str = None, 6 | create_name_function: Callable[[Any], str] = None, 7 | skip: Collection[str] = (), 8 | wrap_return_values: bool = False): ... 9 | -------------------------------------------------------------------------------- /pyproject.toml: -------------------------------------------------------------------------------- 1 | [tool.poetry] 2 | name = "aioify" 3 | version = "0.4.1" 4 | description = "Make every Python function async/await" 5 | authors = ["Yifei Kong ", "Roman Inflianskas "] 6 | license = "MIT" 7 | readme = "README.md" 8 | repository = "https://github.com/yifeikong/aioify" 9 | keywords = ["asyncio", "async", "await", "aioify", "wrap"] 10 | classifiers = [ 11 | "Development Status :: 7 - Inactive", 12 | "Intended Audience :: Developers" 13 | ] 14 | 15 | [tool.poetry.dependencies] 16 | python = "^3.6,<3.10" 17 | module-wrapper = "^0.3.0" 18 | 19 | [tool.poetry.dev-dependencies] 20 | 21 | [build-system] 22 | requires = ["poetry>=0.12"] 23 | build-backend = "poetry.masonry.api" 24 | -------------------------------------------------------------------------------- /setup.cfg: -------------------------------------------------------------------------------- 1 | [metadata] 2 | description-file = README.md 3 | --------------------------------------------------------------------------------