{ "info": { "author": "Quora, Inc.", "author_email": "asynq@quora.com", "bugtrack_url": null, "classifiers": [ "License :: OSI Approved :: Apache Software License", "Programming Language :: Python", "Programming Language :: Python :: 2.7", "Programming Language :: Python :: 3.4", "Programming Language :: Python :: 3.5", "Programming Language :: Python :: 3.6", "Programming Language :: Python :: 3.7" ], "description": ".. image:: http://i.imgur.com/jCPNyOa.png\n\n.. image:: https://travis-ci.org/quora/asynq.svg?branch=master\n :target: https://travis-ci.org/quora/asynq\n\n``asynq`` is a library for asynchronous programming in Python with a focus on batching requests to\nexternal services. It also provides seamless interoperability with synchronous code, support for\nasynchronous context managers, and tools to make writing and testing asynchronous code easier.\n``asynq`` was developed at Quora and is a core component of Quora's architecture. See the original blog\npost `here `_.\n\nThe most important use case for ``asynq`` is batching. For many storage services (e.g., memcache,\nredis) it is far faster to make a single request that fetches many keys at once than to make\nmany requests that each fetch a single key. The ``asynq`` framework makes it easy to write code\nthat takes advantage of batching without radical changes in code structure from code that does not\nuse batching.\n\nFor example, synchronous code to retrieve the names of the authors of a list of Quora answers may\nlook like this:\n\n.. code-block:: python\n\n def all_author_names(aids):\n uids = [author_of_answer(aid) for aid in aids]\n names = [name_of_user(uid) for uid in uids]\n return names\n\nHere, each call to ``author_of_answer`` and ``name_of_user`` would result in a memcache request.\nConverted to use ``asynq``, this code would look like:\n\n.. code-block:: python\n\n @async()\n def all_author_names(aids):\n uids = yield [author_of_answer.async(aid) for aid in aids]\n names = yield [name_of_user.async(uid) for uid in uids]\n result(names); return\n\nAll ``author_of_answer`` calls will be combined into a single memcache request, as will all of the\n``name_of_user`` calls.\n\nFutures\n-------\n\nFutures are the basic building blocks of ``asynq``'s programming model. The scheduler keeps track\nof futures and attempts to schedule them in an efficient way. ``asynq`` uses its own hierarchy of\nFuture classes, rooted in ``asynq.FutureBase``. Futures have a ``.value()`` method that computes\ntheir value if necessary and then returns it.\n\nThe following are the most important Future classes used in ``asynq``:\n\n- ``AsyncTask``, a Future representing the execution of an asynchronous function (see below).\n Normally created by calling ``.async()`` on an asynchronous function.\n- ``ConstFuture``, a Future whose value is known at creation time. This is useful when you need\n to pass a Future somewhere, but no computation is actually needed.\n- ``BatchBase`` and ``BatchItemBase``, the building blocks for doing batching. See below for\n details.\n\n\nDecorators and asynchronous functions\n-------------------------------------\n\n``asynq``'s asynchronous functions are implemented as Python generator functions. Every time an\nasynchronous functions yields one or more Futures, it cedes control the asynq scheduler, which will\nresolve the futures that were yielded and continue running the function after the futures have been\ncomputed.\n\nReturning a value from an asynchronous function is hard in Python 2 because generators are not\nallowed to return a value. ``asynq`` provides the special ``result()`` function that can be used to\nreturn a value from an asynchronous function; it is implemented by raising a custom exception\nthat is caught by the scheduler. At Quora, we instead use a patched Python 2 binary that does\nsupport returning a value from a generator.\n\nThe framework requires usage of the ``@async()`` decorator on all asynchronous functions. This\ndecorator wraps the generator function so that it can be called like a normal, synchronous function.\nIt also creates a ``.async`` attribute on the function that allows calling the function\nasynchronously. Calling this attribute will return an ``AsyncTask`` object corresponding to the\nfunction.\n\nYou can call an asynchronous function synchronously like this:\n\n.. code-block:: python\n\n result = async_fn(a, b)\n\nand asynchronously like this:\n\n.. code-block:: python\n\n result = yield async_fn.async(a, b)\n\nCalling ``async_fn.async(a, b).value()`` has the same result as ``async_fn(a, b)``.\n\nThe decorator has a ``pure=True`` option that disables the ``.async`` attribute and instead makes\nthe function itself asynchronous, so that calling it returns an ``AsyncTask``. We recommend to use\nthis option only in special cases like decorators for asynchronous functions.\n\n``asynq`` also provides an ``@async_proxy()`` decorator for functions that return a Future\ndirectly. Functions decorated with ``@async_proxy()`` look like ``@async()`` functions externally.\nAn example use case is a function that takes either an asynchronous or a synchronous function,\nand calls it accordingly:\n\n.. code-block:: python\n\n @async_proxy()\n def async_call(fn, *args, **kwargs):\n if is_async_fn(fn):\n # Returns an AsyncTask\n return fn.async(*args, **kwargs)\n return ConstFuture(fn(*args, **kwargs))\n\nBatching\n--------\n\nBatching is at the core of what makes ``asynq`` useful. To implement batching, you need to subclass\n``asynq.BatchItemBase`` and ``asynq.BatchBase``. The first represents a single entry in a batch\n(e.g., a single memcache key to fetch) and the second is responsible for executing the batch when\nthe scheduler requests it.\n\nBatch items usually do not require much logic beyond registering themselves with the currently\nactive batch in ``__init__``. Batches need to override the ``_try_switch_active_batch`` method,\nwhich changes the batch that is currently active, and the ``_flush`` method that executes it.\nThis method should call ``.set_value()`` on all the items in the batch.\n\nAn example implementation of batching for memcache is in the ``asynq/examples/batching.py`` file.\nThe framework also provides a ``DebugBatchItem`` for testing.\n\nMost users of ``asynq`` should not need to implement batches frequently. At Quora, we use\nthousands of asynchronous functions, but only five ``BatchBase`` subclasses.\n\nContexts\n--------\n\n``asynq`` provides support for Python context managers that are automatically activated and\ndeactivated when a particular task is scheduled. This feature is necessary because the scheduler\ncan schedule tasks in arbitrary order. For example, consider the following code:\n\n.. code-block:: python\n\n @async()\n def show_warning():\n yield do_something_that_creates_a_warning.async()\n\n @async()\n def suppress_warning():\n with warnings.catch_warnings():\n yield show_warning.async()\n\n @async()\n def caller():\n yield show_warning.async(), suppress_warning.async()\n\nThis code should show only one warning, because only the second call to ``show_warning`` is within\na ``catch_warnings()`` context, but depending on how the scheduler happens to execute these\nfunctions, the code that shows the warning may also be executed while ``catch_warnings()`` is\nactive.\n\nTo remedy this problem, you should use an ``AsyncContext``, which will be automatically paused when\nthe task that created it is no longer active and resumed when it becomes active again. An\n``asynq``-compatible version of ``catch_warnings`` would look something like this:\n\n.. code-block:: python\n\n class catch_warnings(asynq.AsyncContext):\n def pause(self):\n stop_catching_warnings()\n\n def resume(self):\n start_catching_warnings()\n\nDebugging\n---------\n\nBecause the ``asynq`` scheduler is invoked every time an asynchronous function is called, and it\ncan invoke arbitrary other active futures, normal Python stack traces become useless in a\nsufficiently complicated application built on ``asynq``. To make debugging easier, the framework\nprovides the ability to generate a custom ``asynq`` stack trace, which shows how each active\nasynchronous function was invoked.\n\nThe ``asynq.debug.dump_asynq_stack()`` method can be used to print this stack, similar to\n``traceback.print_stack()``. The framework also registers a hook to print out the ``asynq`` stack\nwhen an exception happens.\n\nTools\n-----\n\n``asynq`` provides a number of additional tools to make it easier to write asynchronous code. Some\nof these are in the ``asynq.tools`` module. These tools include:\n\n- ``asynq.async_call`` calls a function asynchronously only if it is asynchronous. This can be\n useful when calling an overridden method that is asynchronous on some child classes but not on others.\n- ``asynq.tools.call_with_context`` calls an asynchronous function within the provided context\n manager. This is helpful in cases where you need to yield multiple tasks at once, but only one\n needs to be within the context.\n- ``asynq.tools.afilter`` and ``asynq.tools.asorted`` are equivalents of the standard ``filter``\n and ``sorted`` functions that take asynchronous functions as their filter and compare functions.\n- ``asynq.tools.acached_per_instance`` caches an asynchronous instance method.\n- ``asynq.tools.deduplicate`` prevents multiple simultaneous calls to the same asynchronous\n function.\n- The ``asynq.mock`` module is an enhancement to the standard ``mock`` module that makes it\n painless to mock asynchronous functions. Without this module, mocking any asynchronous function\n will often also require mocking its ``.async`` attribute. We recommend using ``asynq.mock.patch``\n for all mocking in projects that use ``asynq``.\n- The ``asynq.generator`` module provides an experimental implementation of asynchronous\n generators, which can produce a sequence of values while also using ``asynq``'s batching support.\n\nCompatibility\n-------------\n\n``asynq`` runs on Python 2.7 and Python 3.\n\nTo support Python 3.7, in which ``async`` will be a full keyword, the ``@async()`` decorator and\n``.async`` attribute are aliased to ``@asynq()`` and ``.asynq``.\n\nContributors\n------------\n\n`Alex Yakunin `_, `Jelle Zijlstra `_, `Manan Nayak `_, `Martin Michelsen `_, `Shrey Banga `_, `Suren Nihalani `_, `Suchir Balaji `_ and\nother engineers at Quora.\n\n\n", "description_content_type": "", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/quora/asynq", "keywords": "quora asynq common utility", "license": "Apache Software License", "maintainer": "", "maintainer_email": "", "name": "asynq", "package_url": "https://pypi.org/project/asynq/", "platform": "", "project_url": "https://pypi.org/project/asynq/", "project_urls": { "Homepage": "https://github.com/quora/asynq" }, "release_url": "https://pypi.org/project/asynq/1.1.0/", "requires_dist": [ "Cython (>=0.27.1)", "qcore", "inspect2", "mock; python_version < \"3.3\"" ], "requires_python": "", "summary": "Quora's asynq library", "version": "1.1.0" }, "last_serial": 4154860, "releases": { "0.1.3": [ { "comment_text": "", "digests": { "md5": "9312ea1ee12f2216cab14d20d063115e", "sha256": "dff05350ff64926cc53e9e441345f436d6919403c4e28dc695f505559ce5223d" }, "downloads": -1, "filename": "asynq-0.1.3.tar.gz", "has_sig": false, "md5_digest": "9312ea1ee12f2216cab14d20d063115e", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 537734, "upload_time": "2016-09-19T19:24:23", "url": "https://files.pythonhosted.org/packages/98/84/0de28a45fabca2bf78254792295626aacec91fff8158ab9ecc78cd1066c5/asynq-0.1.3.tar.gz" } ], "0.1.4": [ { "comment_text": "", "digests": { "md5": "570440a73afd2ba218f3154078861301", "sha256": "7b160f066f1c1e82a0ce0d5348547571d93744e81b34ace1d23c26391e063dde" }, "downloads": -1, "filename": "asynq-0.1.4.tar.gz", "has_sig": false, "md5_digest": "570440a73afd2ba218f3154078861301", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 540420, "upload_time": "2017-01-11T00:39:35", "url": "https://files.pythonhosted.org/packages/bd/dd/f34d2e08b42c2f105ba28a1ecbb991a472191d84a6f173949c55728dbcff/asynq-0.1.4.tar.gz" } ], "1.0": [ { "comment_text": "", "digests": { "md5": "3b3ff28b984cb75ad92eb7827f0b6444", "sha256": "46c0b47b8c9c0152f6f3fe207143a77bbacbe53a0d4d32949f03bdeb14e34c06" }, "downloads": -1, "filename": "asynq-1.0.tar.gz", "has_sig": false, "md5_digest": "3b3ff28b984cb75ad92eb7827f0b6444", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 480258, "upload_time": "2017-07-07T19:44:08", "url": "https://files.pythonhosted.org/packages/6d/7f/15ded822841452cee812bc781b62c2c711fdcae07870408b5f533cbae288/asynq-1.0.tar.gz" } ], "1.0.1": [ { "comment_text": "", "digests": { "md5": "0733c0505ebcfcaa6b2539fcc0e03f37", "sha256": "8d29cb53e138524c932eca768f8c62af31b98639667fb336fa37692126e744de" }, "downloads": -1, "filename": "asynq-1.0.1.tar.gz", "has_sig": false, "md5_digest": "0733c0505ebcfcaa6b2539fcc0e03f37", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 45626, "upload_time": "2017-12-13T19:08:42", "url": "https://files.pythonhosted.org/packages/ac/6a/d60ac003808f639f2dd347f3c72835737016e827591074e25f24f961a015/asynq-1.0.1.tar.gz" } ], "1.1.0": [ { "comment_text": "", "digests": { "md5": "ae8b1988792eb447ddab4a2ec0c26ed3", "sha256": "086bcee0d5837bf57e44e86d0e001d9d579fd8b849d2dca9ca51a42e313a3c03" }, "downloads": -1, "filename": "asynq-1.1.0-cp27-cp27m-manylinux1_x86_64.whl", "has_sig": false, "md5_digest": "ae8b1988792eb447ddab4a2ec0c26ed3", "packagetype": "bdist_wheel", "python_version": "cp27", "requires_python": null, "size": 1939548, "upload_time": "2018-08-09T23:47:14", "url": "https://files.pythonhosted.org/packages/18/67/8af1fc0947de38a634098b4bdd7bfe05dee8d47068e2a8cc9be0945c214a/asynq-1.1.0-cp27-cp27m-manylinux1_x86_64.whl" }, { "comment_text": "", "digests": { "md5": "b557af611cb5b8dddf122574efe030ec", "sha256": "ef86a236bab37ebafc3c3abd85d3eb9bb67dfbc05c7d9fd94b7ad65164849385" }, "downloads": -1, "filename": "asynq-1.1.0-cp34-cp34m-manylinux1_x86_64.whl", "has_sig": false, "md5_digest": "b557af611cb5b8dddf122574efe030ec", "packagetype": "bdist_wheel", "python_version": "cp34", "requires_python": null, "size": 2130905, "upload_time": "2018-08-09T23:46:54", "url": "https://files.pythonhosted.org/packages/37/b6/1b4eef0ec9e27a9fbf64649e20cd7ed9c09256127e1b6feca865f1e4e094/asynq-1.1.0-cp34-cp34m-manylinux1_x86_64.whl" }, { "comment_text": "", "digests": { "md5": "540fa7a48adea6566ec5880b4a373dba", "sha256": "f10b3d8f7f8cfe909648141e7dfa6d52424108af85760e3f48cc562b5ba0f385" }, "downloads": -1, "filename": "asynq-1.1.0-cp35-cp35m-manylinux1_x86_64.whl", "has_sig": false, "md5_digest": "540fa7a48adea6566ec5880b4a373dba", "packagetype": "bdist_wheel", "python_version": "cp35", "requires_python": null, "size": 2079735, "upload_time": "2018-08-09T23:46:41", "url": "https://files.pythonhosted.org/packages/45/0b/fb446e41674db1f19f96785f6a0c2840266a707fbff48be01001c162effb/asynq-1.1.0-cp35-cp35m-manylinux1_x86_64.whl" }, { "comment_text": "", "digests": { "md5": "234b09c285a263500d9ef0b96a56dfc5", "sha256": "896740d5aada503dbfc89e7d4aef575bb5a6770dab7c20b3fcaaa45dbcc8b7b4" }, "downloads": -1, "filename": "asynq-1.1.0-cp36-cp36m-manylinux1_x86_64.whl", "has_sig": false, "md5_digest": "234b09c285a263500d9ef0b96a56dfc5", "packagetype": "bdist_wheel", "python_version": "cp36", "requires_python": null, "size": 2170518, "upload_time": "2018-08-09T23:46:24", "url": "https://files.pythonhosted.org/packages/53/6a/032d1ee9f98693df9dd8519220485cc4ab5a26d658bcea5592caf814ab96/asynq-1.1.0-cp36-cp36m-manylinux1_x86_64.whl" }, { "comment_text": "", "digests": { "md5": "13c1a6a814d5e5758f671d9c58b9d9d7", "sha256": "a6be12eb3a8d29e46f198967b17f98db1218ad6615bbc1ddb816628dcddaef16" }, "downloads": -1, "filename": "asynq-1.1.0-cp37-cp37m-manylinux1_x86_64.whl", "has_sig": false, "md5_digest": "13c1a6a814d5e5758f671d9c58b9d9d7", "packagetype": "bdist_wheel", "python_version": "cp37", "requires_python": null, "size": 2145642, "upload_time": "2018-08-09T23:52:27", "url": "https://files.pythonhosted.org/packages/22/e1/7fc9d5639a0b13edb414e33b07016bdf166f5d331132007d7689b1476092/asynq-1.1.0-cp37-cp37m-manylinux1_x86_64.whl" }, { "comment_text": "", "digests": { "md5": "05ecc88016b3e6966905fe5bcaefe02a", "sha256": "bf5fc302f56ea9ff55736240a1f361f559dc79f3b5984493b739034ec86c934b" }, "downloads": -1, "filename": "asynq-1.1.0.tar.gz", "has_sig": false, "md5_digest": "05ecc88016b3e6966905fe5bcaefe02a", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 49929, "upload_time": "2018-08-09T23:52:29", "url": "https://files.pythonhosted.org/packages/ab/d3/46472eaafeb4367a45331e750c8ff960d03b660d772ca6303ff6d0666be6/asynq-1.1.0.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "ae8b1988792eb447ddab4a2ec0c26ed3", "sha256": "086bcee0d5837bf57e44e86d0e001d9d579fd8b849d2dca9ca51a42e313a3c03" }, "downloads": -1, "filename": "asynq-1.1.0-cp27-cp27m-manylinux1_x86_64.whl", "has_sig": false, "md5_digest": "ae8b1988792eb447ddab4a2ec0c26ed3", "packagetype": "bdist_wheel", "python_version": "cp27", "requires_python": null, "size": 1939548, "upload_time": "2018-08-09T23:47:14", "url": "https://files.pythonhosted.org/packages/18/67/8af1fc0947de38a634098b4bdd7bfe05dee8d47068e2a8cc9be0945c214a/asynq-1.1.0-cp27-cp27m-manylinux1_x86_64.whl" }, { "comment_text": "", "digests": { "md5": "b557af611cb5b8dddf122574efe030ec", "sha256": "ef86a236bab37ebafc3c3abd85d3eb9bb67dfbc05c7d9fd94b7ad65164849385" }, "downloads": -1, "filename": "asynq-1.1.0-cp34-cp34m-manylinux1_x86_64.whl", "has_sig": false, "md5_digest": "b557af611cb5b8dddf122574efe030ec", "packagetype": "bdist_wheel", "python_version": "cp34", "requires_python": null, "size": 2130905, "upload_time": "2018-08-09T23:46:54", "url": "https://files.pythonhosted.org/packages/37/b6/1b4eef0ec9e27a9fbf64649e20cd7ed9c09256127e1b6feca865f1e4e094/asynq-1.1.0-cp34-cp34m-manylinux1_x86_64.whl" }, { "comment_text": "", "digests": { "md5": "540fa7a48adea6566ec5880b4a373dba", "sha256": "f10b3d8f7f8cfe909648141e7dfa6d52424108af85760e3f48cc562b5ba0f385" }, "downloads": -1, "filename": "asynq-1.1.0-cp35-cp35m-manylinux1_x86_64.whl", "has_sig": false, "md5_digest": "540fa7a48adea6566ec5880b4a373dba", "packagetype": "bdist_wheel", "python_version": "cp35", "requires_python": null, "size": 2079735, "upload_time": "2018-08-09T23:46:41", "url": "https://files.pythonhosted.org/packages/45/0b/fb446e41674db1f19f96785f6a0c2840266a707fbff48be01001c162effb/asynq-1.1.0-cp35-cp35m-manylinux1_x86_64.whl" }, { "comment_text": "", "digests": { "md5": "234b09c285a263500d9ef0b96a56dfc5", "sha256": "896740d5aada503dbfc89e7d4aef575bb5a6770dab7c20b3fcaaa45dbcc8b7b4" }, "downloads": -1, "filename": "asynq-1.1.0-cp36-cp36m-manylinux1_x86_64.whl", "has_sig": false, "md5_digest": "234b09c285a263500d9ef0b96a56dfc5", "packagetype": "bdist_wheel", "python_version": "cp36", "requires_python": null, "size": 2170518, "upload_time": "2018-08-09T23:46:24", "url": "https://files.pythonhosted.org/packages/53/6a/032d1ee9f98693df9dd8519220485cc4ab5a26d658bcea5592caf814ab96/asynq-1.1.0-cp36-cp36m-manylinux1_x86_64.whl" }, { "comment_text": "", "digests": { "md5": "13c1a6a814d5e5758f671d9c58b9d9d7", "sha256": "a6be12eb3a8d29e46f198967b17f98db1218ad6615bbc1ddb816628dcddaef16" }, "downloads": -1, "filename": "asynq-1.1.0-cp37-cp37m-manylinux1_x86_64.whl", "has_sig": false, "md5_digest": "13c1a6a814d5e5758f671d9c58b9d9d7", "packagetype": "bdist_wheel", "python_version": "cp37", "requires_python": null, "size": 2145642, "upload_time": "2018-08-09T23:52:27", "url": "https://files.pythonhosted.org/packages/22/e1/7fc9d5639a0b13edb414e33b07016bdf166f5d331132007d7689b1476092/asynq-1.1.0-cp37-cp37m-manylinux1_x86_64.whl" }, { "comment_text": "", "digests": { "md5": "05ecc88016b3e6966905fe5bcaefe02a", "sha256": "bf5fc302f56ea9ff55736240a1f361f559dc79f3b5984493b739034ec86c934b" }, "downloads": -1, "filename": "asynq-1.1.0.tar.gz", "has_sig": false, "md5_digest": "05ecc88016b3e6966905fe5bcaefe02a", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 49929, "upload_time": "2018-08-09T23:52:29", "url": "https://files.pythonhosted.org/packages/ab/d3/46472eaafeb4367a45331e750c8ff960d03b660d772ca6303ff6d0666be6/asynq-1.1.0.tar.gz" } ] }