{"id":18004738,"url":"https://github.com/goodmanwen/threadpoolexecutorplus","last_synced_at":"2025-03-26T10:31:36.539Z","repository":{"id":39617896,"uuid":"324874239","full_name":"GoodManWEN/ThreadPoolExecutorPlus","owner":"GoodManWEN","description":"A fully replaceable executor that makes it possible to reuse idle threads and shrink thread list when there's no heavy load.","archived":false,"fork":false,"pushed_at":"2023-08-25T19:31:10.000Z","size":54,"stargazers_count":23,"open_issues_count":2,"forks_count":9,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-21T15:12:27.985Z","etag":null,"topics":["threadpoolexecutor"],"latest_commit_sha":null,"homepage":"","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/GoodManWEN.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}},"created_at":"2020-12-28T00:18:54.000Z","updated_at":"2024-01-29T07:31:53.000Z","dependencies_parsed_at":"2024-10-30T00:36:38.984Z","dependency_job_id":null,"html_url":"https://github.com/GoodManWEN/ThreadPoolExecutorPlus","commit_stats":{"total_commits":40,"total_committers":2,"mean_commits":20.0,"dds":"0.30000000000000004","last_synced_commit":"37c89be47fd75c297ac18030e40934785dcbfe8a"},"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GoodManWEN%2FThreadPoolExecutorPlus","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GoodManWEN%2FThreadPoolExecutorPlus/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GoodManWEN%2FThreadPoolExecutorPlus/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GoodManWEN%2FThreadPoolExecutorPlus/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/GoodManWEN","download_url":"https://codeload.github.com/GoodManWEN/ThreadPoolExecutorPlus/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245636557,"owners_count":20647993,"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":["threadpoolexecutor"],"created_at":"2024-10-30T00:15:48.299Z","updated_at":"2025-03-26T10:31:36.218Z","avatar_url":"https://github.com/GoodManWEN.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ThreadPoolExecutorPlus\n[![fury](https://badge.fury.io/py/ThreadPoolExecutorPlus.svg)](https://badge.fury.io/py/ThreadPoolExecutorPlus)\n[![licence](https://img.shields.io/github/license/GoodManWEN/ThreadPoolExecutorPlus)](https://github.com/GoodManWEN/ThreadPoolExecutorPlus/blob/master/LICENSE)\n[![pyversions](https://img.shields.io/pypi/pyversions/ThreadPoolExecutorPlus.svg)](https://pypi.org/project/ThreadPoolExecutorPlus/)\n[![Publish](https://github.com/GoodManWEN/ThreadPoolExecutorPlus/workflows/Publish/badge.svg)](https://github.com/GoodManWEN/ThreadPoolExecutorPlus/actions?query=workflow:Publish)\n[![Build](https://github.com/GoodManWEN/ThreadPoolExecutorPlus/workflows/Build/badge.svg)](https://github.com/GoodManWEN/ThreadPoolExecutorPlus/actions?query=workflow:Build)\n\nThis package provides you a duck typing of concurrent.futures.ThreadPoolExecutor , which has the very similar api and could fully replace ThreadPoolExecutor in your code.\n\nThe reason why this pack exists is we would like to solve several specific pain spot of memory control in native python one.\n\n## Feature\n- Fully replaceable with concurrent.futures.ThreadPoolExecutor , for example in asyncio.\n- Whenever submit a new task , executor will perfer to use existing idle thread rather than create a new one.\n- Executor will automatically shrink itself duriung leisure time in order to achieve higher efficiency and less memory.\n\n## Install\n\n    pip install ThreadPoolExecutorPlus\n\n## Usage\nSame api as concurrent.futures.ThreadPoolExecutor , with some more control function added:\n\n##### set_daemon_opts(min_workers = None, max_workers = None, keep_alive_time = None)\n    \n\u0026emsp;\u0026emsp;\u0026emsp; In order to guarantee same api interface , new features should be modfied after object created.  \n\u0026emsp;\u0026emsp;\u0026emsp; Could change minimum/maximum activate worker num , and set after how many seconds will the  \n\u0026emsp;\u0026emsp;\u0026emsp; idle thread terminated.   \n\u0026emsp;\u0026emsp;\u0026emsp; By default , min_workers = 4 , max_workers = 16 times cpu_core count on windows and 32x on  \n\u0026emsp;\u0026emsp;\u0026emsp; linux , keep_alive_time = 100s. \n\n## Example\n\nVery the same code in official doc [#threadpoolexecutor-example](https://docs.python.org/3/library/concurrent.futures.html#threadpoolexecutor-example) , with executor replaced:\n```Python3\n# requests_test.py\nimport concurrent.futures\nimport ThreadPoolExecutorPlus\nimport urllib.request\n\nURLS = ['http://www.foxnews.com/',\n        'http://www.cnn.com/',\n        'http://europe.wsj.com/',\n        'http://www.bbc.co.uk/',\n        'http://some-made-up-domain.com/']\n\ndef load_url(url, timeout):\n    with urllib.request.urlopen(url, timeout=timeout) as conn:\n        return conn.read()\n\nwith ThreadPoolExecutorPlus.ThreadPoolExecutor(max_workers=5) as executor:\n    # Try modify deamon options\n    executor.set_daemon_opts(min_workers = 2 , max_workers = 10 , keep_alive_time = 60)\n    future_to_url = {executor.submit(load_url, url, 60): url for url in URLS}\n    for future in concurrent.futures.as_completed(future_to_url):\n        url = future_to_url[future]\n        try:\n            data = future.result()\n        except Exception as exc:\n            print('%r generated an exception: %s' % (url, exc))\n        else:\n            print('%r page is %d bytes' % (url, len(data)))\n```\n\nSame code in offcial doc [#executing-code-in-thread-or-process-pools](https://docs.python.org/3/library/asyncio-eventloop.html#executing-code-in-thread-or-process-pools) with executor replaced:\n```Python3\n# Runs on python version above 3.7\nimport asyncio\nimport ThreadPoolExecutorPlus\n\ndef blocking_io():\n    with open('/dev/urandom', 'rb') as f:\n        return f.read(100)\n\ndef cpu_bound():\n    return sum(i * i for i in range(10 ** 7))\n\nasync def main():\n    loop = asyncio.get_running_loop()\n\n    with ThreadPoolExecutorPlus.ThreadPoolExecutor() as pool:\n        result1 = await loop.run_in_executor(\n            pool, blocking_io)\n        result2 = await loop.run_in_executor(\n            pool, cpu_bound)\n        print('custom thread pool', result1)\n        print('custom thread pool', result2)\n\nasyncio.run(main())\n```\n\nFeature demo:\n```Python3\n# feature_demo.py\nfrom ThreadPoolExecutorPlus import ThreadPoolExecutor\nimport time , datetime\n\ndef log(stmt , name = 'MAIN THREAD'):\n    print(f\"[{datetime.datetime.strftime(datetime.datetime.now() , '%Y-%m-%d %H:%M:%S')}][{name}] {stmt}\")\n\ndef some_func(arg):\n    # does some heavy lifting\n    # outputs some results\n    log(f\"New task triggered in sub thread , sleep {arg} seconds.\" , 'SUB THREAD ')\n    time.sleep(arg)\n    log(f\"Terminated.\" , 'SUB THREAD ') \n    return arg\n\nwith ThreadPoolExecutor() as executor:\n    log(f\"max_workers = {executor._max_workers}\")\n    log(f\"min_workers = {executor._min_workers}\")\n    log(\"====================================================\")\n\n    # We continuously generate tasks which blocks 0.5s every 1 second.\n    # Observe its thread control behaviour.\n    # Thus find it perfer to reuse existing threads.\n    log(\"Reuse test:\")\n    for _ in range(10):\n        executor.submit(some_func , 0.5)\n        time.sleep(1)\n        log(f\"Current poll size = {len(executor._threads)}\")\n\n    log(\"====================================================\")\n\n    # Observe the behaviour after all task done.\n    # Controler will make fast reaction after new options set ,\n    # and automaticlly shrink no-use threads.\n    log(\"Shrink test:\")\n    log(\"Adjust timeout time to 10 seconds.\")\n    executor.set_daemon_opts(min_workers = 2 , max_workers = 10 , keep_alive_time = 10)\n    for _ in range(10):\n        executor.submit(some_func , 3)\n        time.sleep(0.01)\n    log(\"10 new tasks created.\")\n\n\n    time.sleep(3)\n    log(\"All task done.\")\n    \n    for _ in range(15):\n        log(f\"Current poll size = {len(executor._threads)} , {_ + 1}s passed.\")\n        time.sleep(1)\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgoodmanwen%2Fthreadpoolexecutorplus","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgoodmanwen%2Fthreadpoolexecutorplus","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgoodmanwen%2Fthreadpoolexecutorplus/lists"}