threadingbatch


Namethreadingbatch JSON
Version 0.10 PyPI version JSON
download
home_pagehttps://github.com/hansalemaos/threadingbatch
SummaryThread batch with timeout - return values in dict
upload_time2023-01-08 06:55:16
maintainer
docs_urlNone
authorJohannes Fischer
requires_python
licenseMIT
keywords threads kill batch timeout return
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            
# Thread batch with timeout - return values in dict 


1) import threadingbatch
2) add the decorator @threadingbatch.thread_capture to your function
3) add \*args, \*\*kwargs to your function
4) create a list of lists with all function calls 
5) call threadingbatch.start_all_threads
6) get the results from threadingbatch.results

```python
$ pip install threadingbatch
from kthread_sleep import sleep  # killing might not work with time.sleep ( pip install kthread-sleep )
import threadingbatch
import random


@threadingbatch.thread_capture  # The threaded function must be decorated
def test(
    testnumber, *args, **kwargs
):  # a threaded function must have *args, **kwargs and can't have the keyword argument _starttime

    print(f"start {testnumber}")
    sleep(1)
    v = random.randrange(1, 30)
    print(f"end {testnumber}")
    return v


flist = []
for ini, k in enumerate(range(20)):  # creating 20 function calls

    flist.append(
        [
            test,  # function
            (),  # args
            {"testnumber": ini},  # kwargs
            f"function_{str(ini)}",  # key in threadingbatch.results (must be unique and type str), the key can't have the name "done"
        ]
    )
flistt = threadingbatch.start_all_threads(
    flist,
    threadtlimit=5,  # number of simultaneously executed threads
    timeout=4,  # call Kthread.kill after n seconds
    sleepafterkill=0.02,  # sleep time after calling Kthread.kill
    sleepafterstart=0.02,  # sleep time after starting a thread
    ignore_exceptions=False,
    verbose=False,
)

while not threadingbatch.results[
    "done"
]:  # when all threads are done, threadingbatch.results['done'] changes to True

    pass
    sleep(0.1)

# output:
# start 0
# start 1
# start 2
# start 3
# start 4
# start 5
# end 0
# start 6
# end 1
# start 7
# end 2
# start 8
# end 3
# ....

print(threadingbatch.results)
# defaultdict(<function threadingbatch.<lambda>()>,
#             {'done': True,
#              'function_19': defaultdict(<function threadingbatch.<lambda>()>,
#                          {'results': 11, 'realstart': 1673158006.740858}),
#              'function_18': defaultdict(<function threadingbatch.<lambda>()>,
#                          {'results': 23, 'realstart': 1673158006.4810376}),
#              'function_17': defaultdict(<function threadingbatch.<lambda>()>,
#                          {'results': 14, 'realstart': 1673158005.2808566}),
#              'function_16': defaultdict(<function threadingbatch.<lambda>()>,
#                          {'results': 1, 'realstart': 1673158005.1208546}),
#              'function_15': defaultdict(<function threadingbatch.<lambda>()>,
#                          {'results': 4, 'realstart': 1673158005.0807495}),
#              'function_14': defaultdict(<function threadingbatch.<lambda>()>,
#                          {'results': 23, 'realstart': 1673158004.7609262}),
#              'function_13': defaultdict(<function threadingbatch.<lambda>()>,
#                          {'results': 15, 'realstart': 1673158004.7207859}),
#              'function_12': defaultdict(<function threadingbatch.<lambda>()>,
#                          {'results': 4, 'realstart': 1673158004.5210721}),
#              'function_11': defaultdict(<function threadingbatch.<lambda>()>,
#                          {'results': 15, 'realstart': 1673158002.9808593}),
#              'function_10': defaultdict(<function threadingbatch.<lambda>()>,
#                          {'results': 4, 'realstart': 1673158002.941067}),
#              'function_9': defaultdict(<function threadingbatch.<lambda>()>,
#                          {'results': 20, 'realstart': 1673158002.9007895}),
#              'function_8': defaultdict(<function threadingbatch.<lambda>()>,
#                          {'results': 29, 'realstart': 1673158002.420729}),
#              'function_7': defaultdict(<function threadingbatch.<lambda>()>,
#                          {'results': 6, 'realstart': 1673158002.3609126}),
#              'function_6': defaultdict(<function threadingbatch.<lambda>()>,
#                          {'results': 16, 'realstart': 1673158002.3207767}),
#              'function_5': defaultdict(<function threadingbatch.<lambda>()>,
#                          {'results': 1, 'realstart': 1673158000.5775528}),
#              'function_4': defaultdict(<function threadingbatch.<lambda>()>,
#                          {'results': 18, 'realstart': 1673158000.5409462}),
#              'function_3': defaultdict(<function threadingbatch.<lambda>()>,
#                          {'results': 23, 'realstart': 1673158000.5010219}),
#              'function_2': defaultdict(<function threadingbatch.<lambda>()>,
#                          {'results': 14, 'realstart': 1673158000.4609187}),
#              'function_1': defaultdict(<function threadingbatch.<lambda>()>,
#                          {'results': 10, 'realstart': 1673158000.420832}),
#              'function_0': defaultdict(<function threadingbatch.<lambda>()>,
#                          {'results': 12, 'realstart': 1673158000.370694})})



```




            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/hansalemaos/threadingbatch",
    "name": "threadingbatch",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "threads,kill,batch,timeout,return",
    "author": "Johannes Fischer",
    "author_email": "<aulasparticularesdealemaosp@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/6b/de/49d92d81647a70e5930d1469ae70dc4db0f2b807e74874abafb067819c04/threadingbatch-0.10.tar.gz",
    "platform": null,
    "description": "\n# Thread batch with timeout - return values in dict \n\n\n1) import\u00a0threadingbatch\n2) add the decorator\u00a0@threadingbatch.thread_capture to your function\n3) add \\*args, \\*\\*kwargs to your function\n4) create a list of lists with all function calls\u00a0\n5) call\u00a0threadingbatch.start_all_threads\n6) get the results from\u00a0threadingbatch.results\n\n```python\n$ pip install threadingbatch\nfrom kthread_sleep import sleep  # killing might not work with time.sleep ( pip install kthread-sleep )\nimport threadingbatch\nimport random\n\n\n@threadingbatch.thread_capture  # The threaded function must be decorated\ndef test(\n    testnumber, *args, **kwargs\n):  # a threaded function must have *args, **kwargs and can't have the keyword argument _starttime\n\n    print(f\"start {testnumber}\")\n    sleep(1)\n    v = random.randrange(1, 30)\n    print(f\"end {testnumber}\")\n    return v\n\n\nflist = []\nfor ini, k in enumerate(range(20)):  # creating 20 function calls\n\n    flist.append(\n        [\n            test,  # function\n            (),  # args\n            {\"testnumber\": ini},  # kwargs\n            f\"function_{str(ini)}\",  # key in threadingbatch.results (must be unique and type str), the key can't have the name \"done\"\n        ]\n    )\nflistt = threadingbatch.start_all_threads(\n    flist,\n    threadtlimit=5,  # number of simultaneously executed threads\n    timeout=4,  # call Kthread.kill after n seconds\n    sleepafterkill=0.02,  # sleep time after calling Kthread.kill\n    sleepafterstart=0.02,  # sleep time after starting a thread\n    ignore_exceptions=False,\n    verbose=False,\n)\n\nwhile not threadingbatch.results[\n    \"done\"\n]:  # when all threads are done, threadingbatch.results['done'] changes to True\n\n    pass\n    sleep(0.1)\n\n# output:\n# start 0\n# start 1\n# start 2\n# start 3\n# start 4\n# start 5\n# end 0\n# start 6\n# end 1\n# start 7\n# end 2\n# start 8\n# end 3\n# ....\n\nprint(threadingbatch.results)\n# defaultdict(<function threadingbatch.<lambda>()>,\n#             {'done': True,\n#              'function_19': defaultdict(<function threadingbatch.<lambda>()>,\n#                          {'results': 11, 'realstart': 1673158006.740858}),\n#              'function_18': defaultdict(<function threadingbatch.<lambda>()>,\n#                          {'results': 23, 'realstart': 1673158006.4810376}),\n#              'function_17': defaultdict(<function threadingbatch.<lambda>()>,\n#                          {'results': 14, 'realstart': 1673158005.2808566}),\n#              'function_16': defaultdict(<function threadingbatch.<lambda>()>,\n#                          {'results': 1, 'realstart': 1673158005.1208546}),\n#              'function_15': defaultdict(<function threadingbatch.<lambda>()>,\n#                          {'results': 4, 'realstart': 1673158005.0807495}),\n#              'function_14': defaultdict(<function threadingbatch.<lambda>()>,\n#                          {'results': 23, 'realstart': 1673158004.7609262}),\n#              'function_13': defaultdict(<function threadingbatch.<lambda>()>,\n#                          {'results': 15, 'realstart': 1673158004.7207859}),\n#              'function_12': defaultdict(<function threadingbatch.<lambda>()>,\n#                          {'results': 4, 'realstart': 1673158004.5210721}),\n#              'function_11': defaultdict(<function threadingbatch.<lambda>()>,\n#                          {'results': 15, 'realstart': 1673158002.9808593}),\n#              'function_10': defaultdict(<function threadingbatch.<lambda>()>,\n#                          {'results': 4, 'realstart': 1673158002.941067}),\n#              'function_9': defaultdict(<function threadingbatch.<lambda>()>,\n#                          {'results': 20, 'realstart': 1673158002.9007895}),\n#              'function_8': defaultdict(<function threadingbatch.<lambda>()>,\n#                          {'results': 29, 'realstart': 1673158002.420729}),\n#              'function_7': defaultdict(<function threadingbatch.<lambda>()>,\n#                          {'results': 6, 'realstart': 1673158002.3609126}),\n#              'function_6': defaultdict(<function threadingbatch.<lambda>()>,\n#                          {'results': 16, 'realstart': 1673158002.3207767}),\n#              'function_5': defaultdict(<function threadingbatch.<lambda>()>,\n#                          {'results': 1, 'realstart': 1673158000.5775528}),\n#              'function_4': defaultdict(<function threadingbatch.<lambda>()>,\n#                          {'results': 18, 'realstart': 1673158000.5409462}),\n#              'function_3': defaultdict(<function threadingbatch.<lambda>()>,\n#                          {'results': 23, 'realstart': 1673158000.5010219}),\n#              'function_2': defaultdict(<function threadingbatch.<lambda>()>,\n#                          {'results': 14, 'realstart': 1673158000.4609187}),\n#              'function_1': defaultdict(<function threadingbatch.<lambda>()>,\n#                          {'results': 10, 'realstart': 1673158000.420832}),\n#              'function_0': defaultdict(<function threadingbatch.<lambda>()>,\n#                          {'results': 12, 'realstart': 1673158000.370694})})\n\n\n\n```\n\n\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Thread batch with timeout - return values in dict",
    "version": "0.10",
    "split_keywords": [
        "threads",
        "kill",
        "batch",
        "timeout",
        "return"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "bb1eecc7e9a0c0102e278ddfea97dd6ee195712a9d7a5707343433169d86623f",
                "md5": "74b3ae33a911d37575392a3538752ecc",
                "sha256": "0f8b3037663ef0701317a246dc17ec16dbb9fa7d8113498a14e33912d6562726"
            },
            "downloads": -1,
            "filename": "threadingbatch-0.10-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "74b3ae33a911d37575392a3538752ecc",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 7245,
            "upload_time": "2023-01-08T06:55:15",
            "upload_time_iso_8601": "2023-01-08T06:55:15.169358Z",
            "url": "https://files.pythonhosted.org/packages/bb/1e/ecc7e9a0c0102e278ddfea97dd6ee195712a9d7a5707343433169d86623f/threadingbatch-0.10-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "6bde49d92d81647a70e5930d1469ae70dc4db0f2b807e74874abafb067819c04",
                "md5": "b65172f03ffb620bc3cae7a6663cab37",
                "sha256": "c8c663a0711e8a045ae08f9f98dba12ee2d06e11d796baceec59b531ffccbcf8"
            },
            "downloads": -1,
            "filename": "threadingbatch-0.10.tar.gz",
            "has_sig": false,
            "md5_digest": "b65172f03ffb620bc3cae7a6663cab37",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 6022,
            "upload_time": "2023-01-08T06:55:16",
            "upload_time_iso_8601": "2023-01-08T06:55:16.673870Z",
            "url": "https://files.pythonhosted.org/packages/6b/de/49d92d81647a70e5930d1469ae70dc4db0f2b807e74874abafb067819c04/threadingbatch-0.10.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-01-08 06:55:16",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "hansalemaos",
    "github_project": "threadingbatch",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "requirements": [],
    "lcname": "threadingbatch"
}
        
Elapsed time: 0.04360s