{ "info": { "author": "Ricky Cook", "author_email": "pypi@auto.thatpanda.com", "bugtrack_url": null, "classifiers": [ "Development Status :: 3 - Alpha", "Framework :: AsyncIO", "Intended Audience :: Developers", "License :: OSI Approved :: MIT License", "Operating System :: OS Independent", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.5", "Programming Language :: Python :: 3.6", "Programming Language :: Python :: 3.7", "Programming Language :: Python :: 3 :: Only", "Topic :: Software Development :: Libraries" ], "description": "# AIO Task Bound Context\nContext manager that provides a means for context to be set, and retrieved\nin Python AsyncIO.\n\n## What???\nOkay so for a concrete example, thing of how Flask handles the current request:\n\n```python\nfrom flask import request\n```\n\nThis import, called from anywhere, will import the current request being\nhandled. This is made possible in a way similar to this:\n\n```python\nrequest = None\ndef get_request():\n return request\ndef set_request(value):\n global request\n request = value\n```\n\nWhen the HTTP server gets a request, it will call `set_request`, then anywhere\nin the code another function can call `get_request` to get the value.\n\nHere's the kicker: This is not possible with AIO, because multiple tasks may\nbe running at once, so there are multiple values for `request`, rather than\njust a single value. Imagine the same piece of code being used in AIO:\n\n```python\nimport asyncio as aio\n\nasync def handle_request(request):\n set_request(request)\n # generate the response\n await aio.sleep(1)\n assert get_request() == request # will fail\n set_request(None)\n\naio.get_event_loop().run_until_complete(aio.gather(\n handle_request('value 1'),\n handle_request('value 2'),\n))\n```\n\nObviously, this is going to be problematic.\n\n## The answer\n`aio_task_bound_context` attaches a stack of the current context values to the\ncurrent `Task`, as well as tracking the parent tasks so that their context\ncan be inherrited:\n\n```python\nimport asyncio as aio\nfrom aio_task_bound_context import set_task_factory, TaskBoundContext\n\nclass RequestContext(TaskBoundContext):\n def __init__(self, request):\n self.request = request\n def get_value(self):\n return self.request\n\nasync def handle_request(request):\n with RequestContext(request):\n # generate the response\n await aio.sleep(1)\n assert RequestContext.current() == request # will succeed\n\nloop = aio.get_event_loop()\nset_task_factory(loop=loop)\nloop.run_until_complete(aio.gather(\n handle_request('value 1'),\n handle_request('value 2'),\n))\n```\n\n## Examples\n_Note that all these examples will work in async tasks, which is what makes\nthem more special than a simple context manager. They are all simple examples\noutside an async environment, but don't be fooled by the hidden complexity._\n\nTo start off, we need to replace the default task factory in `asyncio` with\na wrapper to add extra details to tasks. Assume this has been executed before\nall examples:\n```python\nimport asyncio as aio\nfrom aio_task_bound_context import create_task_factory, TaskBoundContext\nloop = aio.get_event_loop()\nloop.set_task_factory(create_task_factory(loop=loop))\n```\n\nWith no `get_value` function defined, the \"value\" is the `TaskBoundContext`\nitself, so you can setup values in the `__init__` function if you just want\nto pass around as set of values.\n```python\nclass ExampleContext(TaskBoundContext):\n def __init__(self, *args, **kwargs):\n super().__init__()\n self.args = args\n self.kwargs = kwargs\nwith ExampleContext('an arg', key='in kwargs'):\n assert ExampleContext.current().args == ['an arg']\n assert ExampleContext.current().kwargs == {'key': 'in kwargs'}\n```\n\nThe \"as value\" of the context manager is the value returned from `get_value`.\n```python\nclass ExampleContext(TaskBoundContext):\n def __init__(self, value):\n super().__init__()\n self.value = value\n def get_value(self):\n return self.value\nwith ExampleContext('test') as value:\n assert value == 'test'\nwith ExampleContext('different') as value:\n assert value == 'different'\n```\n\nContexts are a hierarchical stack, so you can have multiple contexts and they\nwill push/pop their values onto/off of the stack of contexts.\n```python\nclass ExampleContext(TaskBoundContext):\n def __init__(self, value):\n super().__init__()\n self.value = value\n def get_value(self):\n return self.value\nwith ExampleContext('first'):\n assert ExampleContext.current() == 'first'\n with ExampleContext('second'):\n assert ExampleContext.current() == 'second'\n assert ExampleContext.current() == 'first\n```\n\nThe `get_value` function can accept a single argument, which is the current\nvalue of the stack.\n```python\nclass LoggerContext(TaskBoundContext):\n def __init__(self, suffix):\n super().__init__()\n self.suffix = suffix\n def get_value(self, current):\n if current is None:\n return logging.getLogger(self.suffix)\n else:\n return current.getChild(self.suffix)\n```\n\n\n## Testing\nPython 3.5+ is supported. To run tests across all environments, we use\n`pyenv`, and some quick `virtualenv` invocations (yes, we could also use\n`tox`).\n\nTo run the tests, just run `./tests_runall.sh` which will install relevant\nPython versions if not already installed, create virtualenvs for them, and\nrun `tests.py`.\n\nTo run tests manually, simply `./test.py`.\n\n## License\nCopyright 2018 Ricky Cook\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.", "description_content_type": "text/markdown", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/rickycook/aio_task_bound_context/", "keywords": "", "license": "", "maintainer": "", "maintainer_email": "", "name": "aio-task-bound-context", "package_url": "https://pypi.org/project/aio-task-bound-context/", "platform": "", "project_url": "https://pypi.org/project/aio-task-bound-context/", "project_urls": { "Homepage": "https://github.com/rickycook/aio_task_bound_context/" }, "release_url": "https://pypi.org/project/aio-task-bound-context/0.2.4/", "requires_dist": null, "requires_python": "", "summary": "Context manager that provides a means for context to be set, and retrieved in Python AsyncIO.", "version": "0.2.4" }, "last_serial": 5370228, "releases": { "0.1.0": [ { "comment_text": "", "digests": { "md5": "dd56b75552e09a50256dd9ace7db755e", "sha256": "5379c885a47027d4b7574e5f68725c5ec4e802a75cfe58819d9931a97b0a7cdc" }, "downloads": -1, "filename": "aio_task_bound_context-0.1.0-py3-none-any.whl", "has_sig": false, "md5_digest": "dd56b75552e09a50256dd9ace7db755e", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 3686, "upload_time": "2018-07-07T08:52:21", "url": "https://files.pythonhosted.org/packages/60/4a/8008ce6df41b891a58db033d9abccdf657e4e9ae54bd2279c5ef8afa42c2/aio_task_bound_context-0.1.0-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "dd3452801ee508b83e5ba4bcedcc78c7", "sha256": "f8467b3a579a094dfa9cf73833ca59dac30d10bdaa560e07326e947a90cd2e92" }, "downloads": -1, "filename": "aio_task_bound_context-0.1.0.tar.gz", "has_sig": false, "md5_digest": "dd3452801ee508b83e5ba4bcedcc78c7", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 3460, "upload_time": "2018-07-07T08:52:22", "url": "https://files.pythonhosted.org/packages/40/d3/f4215f5992a7e24e373a24f7c80dd9a589f39162c7fecc30dc3948411e6d/aio_task_bound_context-0.1.0.tar.gz" } ], "0.2.0": [ { "comment_text": "", "digests": { "md5": "9ab4670fd36ff051e281a2cd6425f3d1", "sha256": "e97c6c9e88907614f3f007941f99383660883d8fba1498c994a7dbfbcf075682" }, "downloads": -1, "filename": "aio_task_bound_context-0.2.0.tar.gz", "has_sig": false, "md5_digest": "9ab4670fd36ff051e281a2cd6425f3d1", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 4558, "upload_time": "2019-05-14T01:51:13", "url": "https://files.pythonhosted.org/packages/12/34/67bde721d949b727dae89ad8252e5c7a3a4b5782420a92e980a657cf4afa/aio_task_bound_context-0.2.0.tar.gz" } ], "0.2.2": [ { "comment_text": "", "digests": { "md5": "73d33fa5b7f5c66dccc9e6c552893486", "sha256": "237e6bef00996ab11900f3ddc2171badc950d3bf12fb9670b5bc5ae1f54b0c5e" }, "downloads": -1, "filename": "aio_task_bound_context-0.2.2.tar.gz", "has_sig": false, "md5_digest": "73d33fa5b7f5c66dccc9e6c552893486", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 4941, "upload_time": "2019-05-14T04:00:56", "url": "https://files.pythonhosted.org/packages/4f/65/5f3de2c4b4515cd8904a146c82f2d0d0f22378b0ec9400606194d6ef02fc/aio_task_bound_context-0.2.2.tar.gz" } ], "0.2.3": [ { "comment_text": "", "digests": { "md5": "1e19379f4bd3ce051b59b04347e45386", "sha256": "b9502fcc1d103cf0f44d6be46e3df8408113b5484798a06ddf75b465e7b9b28f" }, "downloads": -1, "filename": "aio_task_bound_context-0.2.3.tar.gz", "has_sig": false, "md5_digest": "1e19379f4bd3ce051b59b04347e45386", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 4952, "upload_time": "2019-05-14T04:10:05", "url": "https://files.pythonhosted.org/packages/93/11/e92d851c9f8fbad61965ae0ddef81d3c2628903c981d995eca7c56577065/aio_task_bound_context-0.2.3.tar.gz" } ], "0.2.4": [ { "comment_text": "", "digests": { "md5": "623fd7618b5e2f4b34d5fd2a4530ced5", "sha256": "2c0eb1363527116b3489ce85bda3ab9e70707203a7e59e5821327ec44e0084b7" }, "downloads": -1, "filename": "aio_task_bound_context-0.2.4.tar.gz", "has_sig": false, "md5_digest": "623fd7618b5e2f4b34d5fd2a4530ced5", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5202, "upload_time": "2019-06-07T06:12:42", "url": "https://files.pythonhosted.org/packages/da/75/09cc6f4c9a83676ec24124c8ee36224a6e22dcb46e62af3d4ea9a3c7ee1a/aio_task_bound_context-0.2.4.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "623fd7618b5e2f4b34d5fd2a4530ced5", "sha256": "2c0eb1363527116b3489ce85bda3ab9e70707203a7e59e5821327ec44e0084b7" }, "downloads": -1, "filename": "aio_task_bound_context-0.2.4.tar.gz", "has_sig": false, "md5_digest": "623fd7618b5e2f4b34d5fd2a4530ced5", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5202, "upload_time": "2019-06-07T06:12:42", "url": "https://files.pythonhosted.org/packages/da/75/09cc6f4c9a83676ec24124c8ee36224a6e22dcb46e62af3d4ea9a3c7ee1a/aio_task_bound_context-0.2.4.tar.gz" } ] }