{ "info": { "author": "keakon", "author_email": "keakon@gmail.com", "bugtrack_url": null, "classifiers": [ "Development Status :: 4 - Beta", "Environment :: Console", "Intended Audience :: Developers", "License :: OSI Approved :: MIT License", "Operating System :: MacOS :: MacOS X", "Operating System :: POSIX", "Programming Language :: Python :: 2.7", "Programming Language :: Python :: 3", "Programming Language :: Python :: Implementation :: CPython", "Programming Language :: Python :: Implementation :: PyPy" ], "description": "# delayed\n[![Build status](https://travis-ci.org/yizhisec/delayed.svg?branch=master)](https://secure.travis-ci.org/yizhisec/delayed)\n[![Coverage](https://codecov.io/gh/yizhisec/delayed/branch/master/graph/badge.svg)](https://codecov.io/gh/yizhisec/delayed)\n\nDelayed is a simple but robust task queue inspired by [rq](https://python-rq.org/).\n\n## Features\n\n* Robust: all the enqueued tasks will run exactly once, even if the worker got killed at any time.\n* Clean: finished tasks (including failed) won't take the space of your Redis.\n* Distributed: workers as more as needed can run in the same time without further config.\n\n## Requirements\n\n1. Python 2.7 or later, tested on Python 2.7, 3.3 - 3.7 and PyPy 3.5.\n2. UNIX-like systems (with os.fork() implemented, pipe capacity at least 65536 bytes), tested on Ubuntu and macOS.\n3. Redis 2.6.0 or later.\n4. Keep syncing time among all the machines of a task queue.\n\n## Getting started\n\n1. Run a redis server:\n\n ```bash\n $ redis-server\n ```\n\n2. Install delayed:\n\n ```bash\n $ pip install delayed\n ```\n\n3. Create a task queue:\n\n ```python\n import redis\n from delayed.queue import Queue\n\n conn = redis.Redis()\n queue = Queue(name='default', conn=conn)\n ```\n\n4. Three ways to enqueue a task:\n\n * Define a task function and enqueue it:\n\n ```python\n from delayed.delay import delayed\n\n delayed = delayed(queue)\n\n @delayed()\n def delayed_add(a, b):\n return a + b\n\n delayed_add.delay(1, 2) # enqueue delayed_add\n delayed_add.delay(1, b=2) # same as above\n delayed_add(1, 2) # call it immediately\n ```\n\n * Directly enqueue a function:\n\n ```python\n from delayed.delay import delay\n\n delay = delay(queue)\n\n def add(a, b):\n return a + b\n\n delay(add)(1, 2)\n delay(add)(1, b=2) # same as above\n ```\n * Enqueue a predefined task function without importing it:\n\n ```python\n from delayed.task import Task\n\n task = Task(id=None, module_name='test', func_name='add', args=(1, 2))\n queue.enqueue(task)\n ```\n\n5. Run a task worker (or more) in a separated process:\n\n ```python\n import redis\n from delayed.queue import Queue\n from delayed.worker import ForkedWorker\n\n conn = redis.Redis()\n queue = Queue(name='default', conn=conn)\n worker = ForkedWorker(queue=queue)\n worker.run()\n ```\n\n6. Run a task sweeper in a separated process to recovery lost tasks (mainly due to the worker got killed):\n\n ```python\n import redis\n from delayed.queue import Queue\n from delayed.sweeper import Sweeper\n\n conn = redis.Redis()\n queue = Queue(name='default', conn=conn)\n sweeper = Sweeper(queue=queue)\n sweeper.run()\n ```\n\n## QA\n\n1. **Q: What's the limitation on a task function?** \nA: A task function should be defined in module level (except the `__main__` module). Its `args` and `kwargs` should be picklable. \n\n2. **Q: What's the `name` param of a queue?** \nA: It's the key used to store the tasks of the queue. A queue with name \"default\" will use those keys:\n * default: list, enqueued tasks.\n * default_id: str, the next task id.\n * default_noti: list, the same length as enqueued tasks.\n * default_enqueued: sorted set, enqueued tasks with their timeouts.\n * default_dequeued: sorted set, dequeued tasks with their dequeued timestamps.\n\n3. **Q: Why the worker is slow?** \nA: The `ForkedWorker` forks a new process for each new task. So all the tasks are isolated and you won't leak memory. \nTo reduce the overhead of forking processes and importing modules, if your task function code won't be changed in the worker's lifetime, you can switch to `PreforkedWorker`:\n\n ```python\n import redis\n from delayed.queue import Queue\n from delayed.worker import PreforkedWorker\n\n conn = redis.Redis()\n queue = Queue(name='default', conn=conn)\n worker = PreforkedWorker(queue=queue)\n worker.run()\n ```\n\n4. **Q: How does a `ForkedWorker` run?** \nA: It runs such a loop:\n 1. It dequeues a task from the queue periodically.\n 2. It forks a child process to run the task.\n 3. It kills the child process if the child runs out of time.\n 4. When the child process exits, it releases the task.\n\n5. **Q: How does a `PreforkedWorker` run?** \nA: It runs such a loop:\n 1. It dequeues a task from the queue periodically.\n 2. If it has no child process, it forks a new one.\n 3. It sends the task through a pipe to the child.\n 4. It kills the child process if the child runs out of time.\n 5. When the child process exits or it received result from the pipe, it releases the task.\n\n6. **Q: How does the child process of a worker run?** \nA: the child of a `ForkedWorker` just runs the task, unmarks the task as dequeued, then exits.\nThe child of a `PreforkedWorker` runs such a loop:\n 1. It tries to receive a task from the pipe.\n 2. If the pipe has been closed, it exits.\n 3. It runs the task.\n 4. It sends the task result to the pipe.\n 5. It releases the task.\n\n7. **Q: What's lost tasks?** \nA: There are 2 situations a task might get lost:\n * a worker popped a task notification, then got killed before dequeueing the task.\n * a worker dequeued a task, then both the monitor and its child process got killed before they releasing the task.\n\n8. **Q: How to recovery lost tasks?** \nA: Run a sweeper. It dose two things:\n * it keeps the task notification length the same as the task queue.\n * it moves the timeout dequeued tasks back to the task queue.\n\n9. **Q: How to set the timeout of tasks?** \nA: You can set the `default_timeout` of a queue or `timeout` of a task:\n\n ```python\n from delayed.delay import delay_in_time\n\n queue = Queue('default', conn, default_timeout=60)\n\n delayed_add.timeout(10)(1, 2)\n\n delay_in_time = delay_in_time(queue)\n delay_in_time(add, timeout=10)(1, 2)\n ```\n\n10. **Q: How to handle the finished tasks?** \nA: Set the `success_handler` and `error_handler` of the worker. The handlers would be called in a forked process, except the forked process got killed or the monitor process raised an exception.\n\n ```python\n def success_handler(task):\n logging.info('task %d finished', task.id)\n\n def error_handler(task, kill_signal, exc_info):\n if kill_signal:\n logging.error('task %d got killed by signal %d', task.id, kill_signal)\n else:\n logging.exception('task %d failed', exc_info=exc_info)\n\n worker = PreforkedWorker(Queue, success_handler=success_handler, error_handler=error_handler)\n ```\n\n11. **Q: Why does sometimes both `success_handler` and `error_handler` be called for a single task?** \nA: When the child process got killed after the `success_handler` be called, or the monitor process got killed but the child process still finished the task, both handlers would be called. You can consider it as successful.\n\n12. **Q: How to turn on the debug logs?** \nA: Add a `logging.DEBUG` level handler to `delayed.logger.logger`. The simplest way is to call `delayed.logger.setup_logger()`:\n ```python\n from delayed.logger import setup_logger\n\n setup_logger()\n ```\n\n13. **Q: Can I enqueue and dequeue tasks in different Python versions?** \nA: `delayed` uses the `pickle` module to serialize and deserialize tasks.\nIf `pickle.HIGHEST_PROTOCOL` is equal among all your Python runtime, you can use it without any configurations.\nOtherwise you have to choose the lowest `pickle.HIGHEST_PROTOCOL` of all your Python runtime as the pickle protocol.\neg: If you want to enqueue a task in Python 3.7 and dequeue it in Python 2.7. Their `pickle.HIGHEST_PROTOCOL` are `4` and `2`, so you need to set the version to `2`:\n ```python\n from delayed.task import set_pickle_protocol_version\n\n set_pickle_protocol_version(2)\n ```\n\n14. **Q: Why not use JSON or MessagePack to serialize tasks?** \nA: These serializations may confuse some types (eg: `bytes` / `str`, `list` / `tuple`).\n\n15. **Q: What will happen if I changed the pipe capacity?** \nA: `delayed` assumes the pipe capacity is 65536 bytes (the default value on Linux and macOS).\nTo reduce syscalls, it won't check whether the pipe is writable if the length of data to be written is less than 65536.\nIf your system has a lower pipe capacity, the `PreforkedWorker` may not working well for some large task.\nTo fix it, you can set a lower value to `delayed.constants.BUF_SIZE`:\n ```python\n import delayed.constants\n\n delayed.constants.BUF_SIZE = 1024\n ```", "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/yizhisec/delayed", "keywords": "", "license": "", "maintainer": "", "maintainer_email": "", "name": "delayed", "package_url": "https://pypi.org/project/delayed/", "platform": "", "project_url": "https://pypi.org/project/delayed/", "project_urls": { "Homepage": "https://github.com/yizhisec/delayed" }, "release_url": "https://pypi.org/project/delayed/0.5.1b2/", "requires_dist": null, "requires_python": ">=2.7", "summary": "a simple but robust task queue", "version": "0.5.1b2" }, "last_serial": 5643956, "releases": { "0.1.0b1": [ { "comment_text": "", "digests": { "md5": "bd3e2f24eb2e29c79eff7eb1de0fc99b", "sha256": "268ed9839f808ff6905fafad2c79cbfe781b60df3722cf45a1bb9c4717019ecf" }, "downloads": -1, "filename": "delayed-0.1.0b1-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "bd3e2f24eb2e29c79eff7eb1de0fc99b", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": ">=2.7", "size": 10537, "upload_time": "2019-05-28T06:38:51", "url": "https://files.pythonhosted.org/packages/5f/ac/03087f7838ffc6fad6912f8cc5bd215dff2de5b40829ecbd079f0af568bf/delayed-0.1.0b1-py2.py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "4bc5a6b2b26221342d6a2accf2468024", "sha256": "546376d9b94a85bd163122e3b24dcaf94221b991035545c82426fefb0ad521b2" }, "downloads": -1, "filename": "delayed-0.1.0b1.tar.gz", "has_sig": false, "md5_digest": "4bc5a6b2b26221342d6a2accf2468024", "packagetype": "sdist", "python_version": "source", "requires_python": ">=2.7", "size": 10619, "upload_time": "2019-05-28T06:38:54", "url": "https://files.pythonhosted.org/packages/d9/82/c9e9cd30f04414ed03c669c1c8c6bd35a4295ba0967bcff0069f74376f06/delayed-0.1.0b1.tar.gz" } ], "0.1.0b2": [ { "comment_text": "", "digests": { "md5": "258024e0e5281845aa14d053477b5a3f", "sha256": "452d258fe6d40413de4c719303e8e9451b8372e33fda8b7535e41b1d9c806e43" }, "downloads": -1, "filename": "delayed-0.1.0b2-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "258024e0e5281845aa14d053477b5a3f", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": ">=2.7", "size": 10701, "upload_time": "2019-06-03T10:36:13", "url": "https://files.pythonhosted.org/packages/91/a0/b74123de47b2deec93aa22656e294cef9ca53ca1efbdc4c21c3eb647d012/delayed-0.1.0b2-py2.py3-none-any.whl" } ], "0.2.0b1": [ { "comment_text": "", "digests": { "md5": "2a1c609796ee1110c0cd5dd4a6565f17", "sha256": "243e4e609450fe3c2cd6e1874173611b4327e17f07f9fda2b5ed358090769e92" }, "downloads": -1, "filename": "delayed-0.2.0b1-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "2a1c609796ee1110c0cd5dd4a6565f17", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": ">=2.7", "size": 10738, "upload_time": "2019-06-03T11:16:56", "url": "https://files.pythonhosted.org/packages/e7/94/76ff5c4f4d8a719e80c8e92430496c543aaeb5340d7ff3dc7a241197400b/delayed-0.2.0b1-py2.py3-none-any.whl" } ], "0.3.0b2": [ { "comment_text": "", "digests": { "md5": "f3d10f399d0fbcd3d4fdf40d3b532e9d", "sha256": "c374a54d0da3e369302b8f352703bc3df29fb5b657d3c75f97709b03d092ab62" }, "downloads": -1, "filename": "delayed-0.3.0b2-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "f3d10f399d0fbcd3d4fdf40d3b532e9d", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": ">=2.7", "size": 12513, "upload_time": "2019-07-04T01:35:48", "url": "https://files.pythonhosted.org/packages/82/f6/938d4a5e10c5ed498304912f220637ecdbd4caa845b8049851024ad7c15a/delayed-0.3.0b2-py2.py3-none-any.whl" } ], "0.3.1b1": [ { "comment_text": "", "digests": { "md5": "b2c7529a450c91e21a31dc05996f6e4c", "sha256": "62a633273d4aa6db8e8ce2802bb3c908c7379376dd77ea32cfea7282a0d346a1" }, "downloads": -1, "filename": "delayed-0.3.1b1.tar.gz", "has_sig": false, "md5_digest": "b2c7529a450c91e21a31dc05996f6e4c", "packagetype": "sdist", "python_version": "source", "requires_python": ">=2.7", "size": 13232, "upload_time": "2019-08-01T10:03:40", "url": "https://files.pythonhosted.org/packages/85/ce/ac969843cfdaaecc80235233915b77ba32ea3b7257b9192c86daa74c07e1/delayed-0.3.1b1.tar.gz" } ], "0.3.1b2": [ { "comment_text": "", "digests": { "md5": "b6a1602e00b9d1594b361bc2e5f26a8f", "sha256": "f9a0d37d781706e42c893968eca77a23490c4a9a44f11f520485ce6e6a51fcb7" }, "downloads": -1, "filename": "delayed-0.3.1b2.tar.gz", "has_sig": false, "md5_digest": "b6a1602e00b9d1594b361bc2e5f26a8f", "packagetype": "sdist", "python_version": "source", "requires_python": ">=2.7", "size": 13280, "upload_time": "2019-08-02T07:00:25", "url": "https://files.pythonhosted.org/packages/20/45/01b9bd48eecef8f62cb7a844ac5d34a89a4b6bb5c72d03362db432779d24/delayed-0.3.1b2.tar.gz" } ], "0.4.0b1": [ { "comment_text": "", "digests": { "md5": "c14d0fc309d6aa7a5c35082ae57446b4", "sha256": "72ea2f1cb083f8c252c586fe99c469142320e36c32cf3d2fa523a3f5dd7c2c0c" }, "downloads": -1, "filename": "delayed-0.4.0b1.tar.gz", "has_sig": false, "md5_digest": "c14d0fc309d6aa7a5c35082ae57446b4", "packagetype": "sdist", "python_version": "source", "requires_python": ">=2.7", "size": 13417, "upload_time": "2019-08-05T05:55:15", "url": "https://files.pythonhosted.org/packages/59/f4/ded62e7723b26d62175ddadc0d8f7bbf1a64e14c3ddafcf8712f60212a43/delayed-0.4.0b1.tar.gz" } ], "0.5.0b1": [ { "comment_text": "", "digests": { "md5": "24ee640326653ff30012cca8286f0e57", "sha256": "3cba84e20a9ca279cf2bd817f835fbcf2e09bc10b7a441e8f902a9919b9d0df8" }, "downloads": -1, "filename": "delayed-0.5.0b1.tar.gz", "has_sig": false, "md5_digest": "24ee640326653ff30012cca8286f0e57", "packagetype": "sdist", "python_version": "source", "requires_python": ">=2.7", "size": 14551, "upload_time": "2019-08-06T15:57:32", "url": "https://files.pythonhosted.org/packages/fc/76/ce7de4fe3261c5f7c423afe7a74c93bb4c1bf48169cc7d555e7ec4aa0a99/delayed-0.5.0b1.tar.gz" } ], "0.5.0b2": [ { "comment_text": "", "digests": { "md5": "2d93645d8a1b45ed4d9cf6fd23752282", "sha256": "fcbe3ff6258904d0c192429ddcf531ca52574bf0938237214f9ed7825e8ed00a" }, "downloads": -1, "filename": "delayed-0.5.0b2.tar.gz", "has_sig": false, "md5_digest": "2d93645d8a1b45ed4d9cf6fd23752282", "packagetype": "sdist", "python_version": "source", "requires_python": ">=2.7", "size": 14695, "upload_time": "2019-08-07T04:37:18", "url": "https://files.pythonhosted.org/packages/07/95/87fbf4703f0d832d437f8ae8b48a054abd3ee1edbf5f7dfd9fb9caae982c/delayed-0.5.0b2.tar.gz" } ], "0.5.1b2": [ { "comment_text": "", "digests": { "md5": "b7b232c8274ef9053b226776fe454f8f", "sha256": "4c13891acfa613cf9c93e7ead880673dd980ad7491e03a2289f9c0621a970b14" }, "downloads": -1, "filename": "delayed-0.5.1b2.tar.gz", "has_sig": false, "md5_digest": "b7b232c8274ef9053b226776fe454f8f", "packagetype": "sdist", "python_version": "source", "requires_python": ">=2.7", "size": 15749, "upload_time": "2019-08-07T09:08:48", "url": "https://files.pythonhosted.org/packages/cb/90/89357037957c1d95bf1ef5b09a312e79c95411ca1dddc252d063829e32f1/delayed-0.5.1b2.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "b7b232c8274ef9053b226776fe454f8f", "sha256": "4c13891acfa613cf9c93e7ead880673dd980ad7491e03a2289f9c0621a970b14" }, "downloads": -1, "filename": "delayed-0.5.1b2.tar.gz", "has_sig": false, "md5_digest": "b7b232c8274ef9053b226776fe454f8f", "packagetype": "sdist", "python_version": "source", "requires_python": ">=2.7", "size": 15749, "upload_time": "2019-08-07T09:08:48", "url": "https://files.pythonhosted.org/packages/cb/90/89357037957c1d95bf1ef5b09a312e79c95411ca1dddc252d063829e32f1/delayed-0.5.1b2.tar.gz" } ] }