{"id":15576428,"url":"https://github.com/njsmith/deadline-scopes","last_synced_at":"2025-04-09T17:45:24.232Z","repository":{"id":145901799,"uuid":"117073015","full_name":"njsmith/deadline-scopes","owner":"njsmith","description":null,"archived":false,"fork":false,"pushed_at":"2018-01-11T09:41:07.000Z","size":3,"stargazers_count":8,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-23T19:51:21.465Z","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":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/njsmith.png","metadata":{"files":{"readme":"README.rst","changelog":null,"contributing":null,"funding":null,"license":null,"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-01-11T08:39:17.000Z","updated_at":"2020-09-03T12:29:40.000Z","dependencies_parsed_at":null,"dependency_job_id":"bf3576f4-b2dd-4875-a1a8-ee70bd84e5fa","html_url":"https://github.com/njsmith/deadline-scopes","commit_stats":{"total_commits":3,"total_committers":1,"mean_commits":3.0,"dds":0.0,"last_synced_commit":"dadf349f8eb028aabb11cde329a3bde6352709b2"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/njsmith%2Fdeadline-scopes","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/njsmith%2Fdeadline-scopes/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/njsmith%2Fdeadline-scopes/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/njsmith%2Fdeadline-scopes/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/njsmith","download_url":"https://codeload.github.com/njsmith/deadline-scopes/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248080846,"owners_count":21044573,"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":"2024-10-02T18:49:07.313Z","updated_at":"2025-04-09T17:45:24.216Z","avatar_url":"https://github.com/njsmith.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"========================================\n Deadline scopes for synchronous Python\n========================================\n\nThis is a proof-of-concept implementation of Trio-style cancel scopes\nfor synchronous Python code, as described in my post `Timeouts and\ncancellation for humans\n\u003chttps://vorpus.org/blog/timeouts-and-cancellation-for-humans/\u003e`__. It\nmakes it easy to implement abstractable, composable, user-friendly\ntimeout support.\n\nCompared to Trio's cancel scopes, this library has a limitation: it\nonly supports time-based cancellation, not cancellation in response to\narbitrary events (that is, there's no ``cancel_scope.cancel()``\nmethod) – that's why I call it \"deadline scopes\" instead of \"cancel\nscopes\". This limitation is imposed by the underlying Python standard\nlibrary primitives it uses. However, this mostly isn't a big deal,\nsince if a single-threaded synchronous program is stuck inside some\nblocking operation, then who would call cancel anyway?\n\nThis is an experiment, and I'm not sure how much time I'll spend\ndeveloping it further – see `Trio \u003chttps://trio.readthedocs.io\u003e`__ for\na more complete Python I/O solution. But if you want to pick it up and\nrun with it, then go for it. Or I guess it wouldn't be too hard to add\nsupport to ``requests``, since `we're working on rewriting its I/O\nlayer anyway... hmm \u003chttps://github.com/njsmith/urllib3/issues/1\u003e`__.\n\n\nExample\n=======\n\n.. code-block:: python\n\n   from deadline_scopes import deadline_socket, fail_after\n\n   sock = deadline_socket()  # like socket.socket()\n\n   # 5 second timeout on everything inside this block\n   with fail_after(5):\n       # Make a request to http://httpbin.org/delay/10, which should\n       # take 10 seconds to complete\n       print(\"connection\")\n       sock.connect((\"httpbin.org\", 80))\n       print(\"sending\")\n       sock.sendall(b\"GET /delay/10 HTTP/1.1\\r\\nHost: httpbin.org\\r\\n\\r\\n\")\n       while True:\n           # Read and discard body\n           data = sock.recv(1024)\n           print(data)\n           if not data:\n               break\n\nSee the code for more details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnjsmith%2Fdeadline-scopes","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnjsmith%2Fdeadline-scopes","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnjsmith%2Fdeadline-scopes/lists"}