{"id":25439302,"url":"https://github.com/joaduo/async_cast","last_synced_at":"2025-11-01T09:30:21.568Z","repository":{"id":57412065,"uuid":"385363059","full_name":"joaduo/async_cast","owner":"joaduo","description":"Cast async function sync (blocking) and viceversa. Also run in threads sync and async functions.","archived":false,"fork":false,"pushed_at":"2023-12-19T10:05:23.000Z","size":34,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-09-23T09:12:59.644Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/joaduo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-07-12T19:37:52.000Z","updated_at":"2023-12-19T09:07:11.000Z","dependencies_parsed_at":"2022-09-07T23:31:38.531Z","dependency_job_id":null,"html_url":"https://github.com/joaduo/async_cast","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joaduo%2Fasync_cast","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joaduo%2Fasync_cast/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joaduo%2Fasync_cast/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joaduo%2Fasync_cast/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/joaduo","download_url":"https://codeload.github.com/joaduo/async_cast/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239269779,"owners_count":19610871,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-02-17T10:19:00.872Z","updated_at":"2025-11-01T09:30:21.278Z","avatar_url":"https://github.com/joaduo.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# async_cast\n\n[![.github/workflows/tests.yml](https://github.com/joaduo/async_cast/actions/workflows/tests.yml/badge.svg)](https://github.com/joaduo/async_cast/actions/workflows/tests.yml)\n\nCast async function to blocking and viceversa. (works on python 3.7 and up)\nAlso run functions in threads, whether async or blocking.\n\n## Why `async_cast`?\n\nI found the current `asyncio` tools frustrating when migrating from blocking code.\nIf you want to profit IO operations to *\"run something else\"* you need to rewrite all code to use\nasyncio. When you start form scratch that's acceptable, but not with legacy code.\n\nSo with this small self-contained library you can easily convert legacy code into async code.\nThe best way is running several blocking function in different threads. But **be aware** that threads\nalso bring race conditions, so make sure concurrent functions are thread-safe.\n\n## Installing\n\nhttps://pypi.org/project/async-cast/\n\n```\npip install -U async-cast\n```\n\nThe package is a single module that you can easily audit. \n\n## Decorator: casting `async` function to a blocking function\n\n```python\nfrom async_cast import also_blocking\n\n@also_blocking\nasync def request_url(url, **kwargs):\n    print(f'Requesting {url} with options {kwargs}')\n    ...\n    result = f'\u003ch1\u003e{url}\u003c/h1\u003e'\n    return result\n\nif __name__ == '__main__':\n    print(request_url.blocking('https://github.com'))\n```\n\n## Decorator: casting a blocking function to `async` function\n\n```python\nfrom async_cast import also_async\nimport asyncio\n\n@also_async\ndef request_url(url, **kwargs):\n    print(f'Requesting {url} with options {kwargs}')\n    ...\n    result = f'\u003ch1\u003e{url}\u003c/h1\u003e'\n    return result\n\nasync def main():\n    print(await request_url.async_('https://github.com'))\n\nif __name__ == '__main__':\n    asyncio.run(main())\n```\n\n## Running tasks in a ThreadPool\n\nI wrapped existing `ThreadPoolExecutor` to make it easier to run tasks inside it.\nTasks are automatically registered in the pool declared by the `with thread_pool(...):` context.\n\n### Running `async` function in threadpool\n\n```python\nfrom async_cast import also_blocking, thread_pool\nimport asyncio\n\n@also_blocking\nasync def request_url(url, **kwargs):\n    print(f'Requesting {url} with options {kwargs}')\n    ...\n    result = f'\u003ch1\u003e{url}\u003c/h1\u003e'\n    return result\n\nasync def main():\n    with thread_pool(3):\n        t1 = request_url.async_thread('https://github.com')\n        t2 = request_url.async_thread('https://google.com')\n        t3 = request_url.async_thread('https://facebook.com')\n        results = await asyncio.gather(t1,t2,t3)\n        print(results)\n\nif __name__ == '__main__':\n    asyncio.run(main())\n```\n\n### Running blocking function in threadpool\n\n```python\nfrom async_cast import also_async, thread_pool\nimport asyncio\n\n@also_async\ndef request_url(url, **kwargs):\n    print(f'Requesting {url} with options {kwargs}')\n    ...\n    result = f'\u003ch1\u003e{url}\u003c/h1\u003e'\n    return result\n\nasync def main():\n    with thread_pool(3):\n        t1 = request_url.async_thread('https://github.com')\n        t2 = request_url.async_thread('https://google.com')\n        t3 = request_url.async_thread('https://facebook.com')\n        results = await asyncio.gather(t1,t2,t3)\n        print(results)\n\nif __name__ == '__main__':\n    asyncio.run(main())\n```\n## Non decorators alternatives\n\n## Casting `async` function to a blocking function with `to_blocking`\n\n```python\nfrom async_cast import to_blocking\n\nasync def request_url(url, **kwargs):\n    print(f'Requesting {url} with options {kwargs}')\n    ...\n    result = f'\u003ch1\u003e{url}\u003c/h1\u003e'\n    return result\n\nif __name__ == '__main__':\n    print(to_blocking(request_url)('https://github.com'))\n```\n\n## Casting a blocking function to `async` function with `to_async`\n\n```python\nfrom async_cast import to_async\nimport asyncio\n\ndef request_url(url, **kwargs):\n    print(f'Requesting {url} with options {kwargs}')\n    ...\n    result = f'\u003ch1\u003e{url}\u003c/h1\u003e'\n    return result\n\nasync def main():\n    print(await to_async(request_url)('https://github.com'))\n\nif __name__ == '__main__':\n    asyncio.run(main())\n```\n\n### Running `async` or blocking function in threadpool with `to_async_thread`\n\n```python\nfrom async_cast import to_async_thread, thread_pool\nimport asyncio\n\nasync def request_url(url, **kwargs):\n    print(f'Requesting {url} with options {kwargs}')\n    ...\n    result = f'\u003ch1\u003e{url}\u003c/h1\u003e'\n    return result\n\ndef request_url_blocking(url, **kwargs):\n    print(f'Requesting {url} with options {kwargs}')\n    ...\n    result = f'\u003ch1\u003e{url}\u003c/h1\u003e'\n    return result\n\nasync def main():\n    with thread_pool(3):\n        t1 = to_async_thread(request_url)('https://github.com')\n        t2 = to_async_thread(request_url)('https://google.com')\n        t3 = to_async_thread(request_url)('https://facebook.com')\n        t4 = to_async_thread(request_url_blocking)('https://duckduckgo.com')\n        results = await asyncio.gather(t1,t2,t3,t4)\n        print(results)\n\nif __name__ == '__main__':\n    asyncio.run(main())\n```\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjoaduo%2Fasync_cast","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjoaduo%2Fasync_cast","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjoaduo%2Fasync_cast/lists"}