{"id":46152414,"url":"https://github.com/static-frame/conditional-futures","last_synced_at":"2026-03-02T09:02:49.941Z","repository":{"id":325004904,"uuid":"1099442048","full_name":"static-frame/conditional-futures","owner":"static-frame","description":null,"archived":false,"fork":false,"pushed_at":"2025-11-19T03:52:33.000Z","size":5,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-11-19T04:12:51.623Z","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/static-frame.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2025-11-19T02:01:40.000Z","updated_at":"2025-11-19T03:52:36.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/static-frame/conditional-futures","commit_stats":null,"previous_names":["static-frame/conditional-futures"],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/static-frame/conditional-futures","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/static-frame%2Fconditional-futures","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/static-frame%2Fconditional-futures/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/static-frame%2Fconditional-futures/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/static-frame%2Fconditional-futures/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/static-frame","download_url":"https://codeload.github.com/static-frame/conditional-futures/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/static-frame%2Fconditional-futures/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29996278,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-02T01:47:34.672Z","status":"online","status_checked_at":"2026-03-02T02:00:07.342Z","response_time":60,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":"2026-03-02T09:02:49.119Z","updated_at":"2026-03-02T09:02:49.895Z","avatar_url":"https://github.com/static-frame.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# conditional-futures\n\nMake multi-threaded concurrency backward- and forward-compatible for the free-threaded future of Python.\n\n\n## Multi-Threading In and Out of Free-Threading\n\nThe following is a table of performance results for the execution of a function across each row of a NumPy array ([code](#example)), with (no [GIL](https://docs.python.org/3/glossary.html#term-global-interpreter-lock)) `python3.14t` and (GIL enabled) `python3.14`, and with and without `ThreadPoolExecutor`. Performance improves with `python3.14t` but degrades with `python3.14`.\n\n|Interpreter |Executor                      |Duration|\n|------------|------------------------------|--------|\n|python3.14t |None                          |🟡 0.577 |\n|python3.14t |ThreadPoolExecutor            |🟢 0.34  |\n|python3.14  |None                          |🟡 0.544 |\n|python3.14  |ThreadPoolExecutor            |🔴 2.231 |\n\n`ConditionalThreadPoolExecutor` provides a single interface to get the best result in either context.\n\n|Interpreter |Executor                      |Duration|\n|------------|------------------------------|--------|\n|python3.14t |None                          |🟡 0.577 |\n|python3.14t |ConditionalThreadPoolExecutor |🟢 0.339 |\n|python3.14  |None                          |🟡 0.544 |\n|python3.14  |ConditionalThreadPoolExecutor |🟡 0.532 |\n\n\n## Introduction\n\nThe new free-threaded version of Python (with the GIL disabled) offers extraordinary performance improvements in  multi-threading CPU-bound processes. Upgrading your code to take advantage of this performance, however, is problematic. The same multi-threaded code, if run with the GIL enabled, can actually perform significantly worse than single-threaded execution. Even when using a free-threaded interpreter, importing an incompatible C-extension will automatically re-enable the GIL.\n\nFor code that will run across many interpreters with or without the GIL, we need interfaces that perform multi-threaded processing only when the GIL is disabled.\n\nThe `conditional-futures` package provides `ConditionalThreadPoolExecutor`, a drop-in replacement for `ThreadPoolExecutor` that adapts based on the runtime state of the GIL.\n\nWhen running under free-threaded Python with the GIL disabled `ConditionalThreadPoolExecutor` behaves like a normal thread pool. When running under a GIL-enabled build, it falls back on single-threaded execution, potentially avoiding a significant degradation in performance. The same implementation offers optimal performance in all contexts.\n\nNote that, even with the GIL enabled, multi-threading can perform well for I/O-bound processes. `ConditionalThreadPoolExecutor` is appropriate only for CPU-bound processes that perform worse with the GIL.\n\n\n## Example\n\nThe performance of function application on the rows of a 2D NumPy array can be used to show both the benefits of free-threaded Python and the need for `ConditionalThreadPoolExecutor`.\n\nFirst, using the free-threaded build of Python 3.14, we can create an array and apply a function to each row of that array. The `ipython` `%time` utility is used to measure duration.\n\n```python\n$ python3.14t\n\u003e\u003e\u003e array = np.arange(100_000_000).reshape(100_000, 1_000)\n\u003e\u003e\u003e func = lambda row: (row[row % 2 == 0]**2).sum()\n\u003e\u003e\u003e %time _ = np.fromiter((func(row) for row in array), dtype=float, count=array.shape[0])\nCPU times: user 580 ms, sys: 662 μs, total: 580 ms\nWall time: 581 ms\n```\n\nUsing `ConditionalThreadPoolExecutor` with this GIL-disabled build of Python we can take advantage of multi-threaded performance on a CPU-bound process: the same routine is almost twice as fast:\n\n```python\n\u003e\u003e\u003e with ConditionalThreadPoolExecutor(max_workers=4) as ex:\n...     %time _ = np.fromiter(ex.map(func, array), dtype=float, count=array.shape[0])\n...\nCPU times: user 1.31 s, sys: 98 ms, total: 1.41 s\nWall time: 352 ms\n```\n\nNow, if using the standard Python 3.14 interpreter (with the GIL enabled), `ThreadPoolExecutor` degrades performance: the same operation takes six times as long!\n\n```python\n$ python3.14\n\u003e\u003e\u003e array = np.arange(100_000_000).reshape(100_000, 1_000)\n\u003e\u003e\u003e func = lambda row: (row[row % 2 == 0] ** 2).sum()\n\u003e\u003e\u003e with ThreadPoolExecutor(max_workers=4) as ex:\n...     %time _ = np.fromiter(ex.map(func, array), dtype=float, count=array.shape[0])\n...\nCPU times: user 1.9 s, sys: 2.21 s, total: 4.12 s\nWall time: 2.33 s\n```\n\nUsing `ConditionalThreadPoolExecutor`, one implementation performs optimally in both contexts. Running the same code with `python3.14`, `ConditionalThreadPoolExecutor` does not perform as well as with `python3.14t`, but provides the best option available: single-threaded performance.\n\n\n```python\n\u003e\u003e\u003e with ConditionalThreadPoolExecutor(max_workers=4) as ex:\n...     %time _ = np.fromiter(ex.map(func, array), dtype=float, count=array.shape[0])\n...\nCPU times: user 532 ms, sys: 773 μs, total: 533 ms\nWall time: 533 ms\n```\n\n\n## Installation\n\n```bash\npip install conditional-futures\n```\n\n\n## What is New in `conditional-futures`\n\n### 1.0.2\n\nExtended documentation.\n\n\n### 1.0.1\n\nExtended documentation.\n\n\n### 1.0.0\n\nInitial release.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstatic-frame%2Fconditional-futures","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstatic-frame%2Fconditional-futures","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstatic-frame%2Fconditional-futures/lists"}