{"id":16649998,"url":"https://github.com/mosquito/forklib","last_synced_at":"2025-07-22T11:33:11.595Z","repository":{"id":66117648,"uuid":"142293038","full_name":"mosquito/forklib","owner":"mosquito","description":"Fork the single process easily","archived":false,"fork":false,"pushed_at":"2020-05-12T09:58:51.000Z","size":28,"stargazers_count":4,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-09T17:34:47.906Z","etag":null,"topics":["fork","linux","posix","python"],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mosquito.png","metadata":{"files":{"readme":"README.rst","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":"2018-07-25T11:52:16.000Z","updated_at":"2023-10-03T12:07:08.000Z","dependencies_parsed_at":"2023-02-20T18:00:58.802Z","dependency_job_id":null,"html_url":"https://github.com/mosquito/forklib","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/mosquito/forklib","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mosquito%2Fforklib","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mosquito%2Fforklib/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mosquito%2Fforklib/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mosquito%2Fforklib/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mosquito","download_url":"https://codeload.github.com/mosquito/forklib/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mosquito%2Fforklib/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266483608,"owners_count":23936379,"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","status":"online","status_checked_at":"2025-07-22T02:00:09.085Z","response_time":66,"last_error":null,"robots_txt_status":null,"robots_txt_updated_at":null,"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":["fork","linux","posix","python"],"created_at":"2024-10-12T09:14:49.307Z","updated_at":"2025-07-22T11:33:11.572Z","avatar_url":"https://github.com/mosquito.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"forklib\n=======\n\nFork the single process easily\n\nBasic example\n+++++++++++++\n\n.. code-block:: python\n\n    import forklib\n    import logging\n    import os\n    from time import sleep\n\n\n    logging.basicConfig(level=logging.DEBUG)\n\n    def run():\n        print(\n            \"Proceess #{id} has PID: {pid}\".format(\n                id=forklib.get_id(),\n                pid=os.getpid()\n            )\n        )\n        sleep(1)\n\n\n    def main():\n        print(\"Master proccess has PID: {0}\".format(os.getpid()))\n        forklib.fork(4, run)\n\n\n\n    if __name__ == '__main__':\n        main()\n\n\nThis code makes 4 forks. When you try to run it you will see something like this ::\n\n    Master proccess has PID: 39485\n    DEBUG:forklib.forking:Starting 4 processes\n    Proceess #1 has PID: 39487\n    Proceess #0 has PID: 39486\n    Proceess #2 has PID: 39488\n    Proceess #3 has PID: 39489\n    DEBUG:forklib.forking:Child with PID: 39487 Number: 1 exited normally\n    DEBUG:forklib.forking:Child with PID: 39489 Number: 3 exited normally\n    DEBUG:forklib.forking:Child with PID: 39488 Number: 2 exited normally\n    DEBUG:forklib.forking:Child with PID: 39486 Number: 0 exited normally\n\n\nForkme will be control forks. When subprocess will be killed or will exit with\nnon-zero code it will be restarted immediately. e.g.::\n\n    Master proccess has PID: 7579\n    INFO:forklib:Starting 4 processes\n    Proceess #0 has PID: 7580\n    Proceess #1 has PID: 7581\n    Proceess #2 has PID: 7582\n    Proceess #3 has PID: 7583\n    WARNING:forklib:Child with PID: 7580 Number: 0 killed by signal 9, restarting\n    Proceess #0 has PID: 7584\n\n\n``async_callback`` example\n++++++++++++++++++++++++++\n\n.. code-block:: python\n\n    import asyncio\n    import forklib\n    import logging\n    import os\n    from time import sleep\n\n\n    logging.basicConfig(level=logging.DEBUG)\n\n    def run():\n        print(\n            \"Proceess #{id} has PID: {pid}\".format(\n                id=forklib.get_id(),\n                pid=os.getpid()\n            )\n        )\n        sleep(1)\n\n    async def amain():\n        await asyncio.sleep(0.5)\n        print(\"Async callback finished\")\n\n\n    def main():\n        print(\"Master proccess has PID: {0}\".format(os.getpid()))\n\n        forklib.fork(\n            4, run,\n            async_callback=amain,\n            # Wait all incomplete async tasks, otherwise cancel (default)\n            wait_async_callback = True,\n        )\n\n\n\n    if __name__ == '__main__':\n        main()\n\n\n``thread_callback`` example\n+++++++++++++++++++++++++++\n\n.. code-block:: python\n\n   import asyncio\n   import logging\n   import os\n   from time import sleep\n   from threading import Event\n\n   import forklib\n\n\n   logging.basicConfig(level=logging.DEBUG)\n\n\n   def run():\n       print(\n           \"Proceess #{id} has PID: {pid}\".format(\n               id=forklib.get_id(),\n               pid=os.getpid(),\n           ),\n       )\n       sleep(3)\n\n\n   exit_event = Event()\n\n\n   def thread_callback():\n       while not exit_event.is_set():\n           sleep(0.5)\n           print(\"Thread callback making great stuff\")\n       print(\"Thread callback finished\")\n\n\n   async def async_callback():\n       await asyncio.sleep(5)\n       print(\"Async callback finished\")\n\n\n   def main():\n       print(\"Master proccess has PID: {0}\".format(os.getpid()))\n       forklib.fork(\n           4, run,\n           thread_callback=thread_callback,\n\n           # Wait theread_callback, otherwise exit (default)\n           # Note: You have to be careful when using this option.\n           # Thread cancellation is impossible in the general case and you must\n           # implement your own way of thread exit notification for example\n           # like following one using exit_callback and threading.Event\n           wait_thread_callback=True,\n\n           # Notifying thread_callback about exit.\n           exit_callback=exit_event.set,\n\n           async_callback=async_callback,\n           # Wait async_callback, otherwise cancel incomplete tasks (default)\n           wait_async_callback=True\n       )\n\n\n   if __name__ == \"__main__\":\n       main()\n\n\nParallel iteration\n++++++++++++++++++\n\nYou can load the large array of elements on the memory and process it in\nmultiple processes. After forking the memory will not be copied, instead\nof the copy-on-write mechanism will be used.\n\n.. code-block:: python\n\n   from forklib import fork_map\n   import logging\n\n\n   logging.basicConfig(level=logging.INFO)\n\n\n   def map_func(item):\n       return item + 1\n\n\n   def main():\n       for item in fork_map(map_func, range(20000), workers=10):\n           print(item)\n\n\n   if __name__ == '__main__':\n       main()\n\n\n\nVersioning\n++++++++++\n\nThis software follows `Semantic Versioning`_\n\n.. _Semantic Versioning: http://semver.org/\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmosquito%2Fforklib","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmosquito%2Fforklib","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmosquito%2Fforklib/lists"}