Package: django-gearman
django-gearman==============*django-gearman* is a convenience wrapper for the [Gearman][Gearman][Python Bindings][python-gearman].With django-gearman, you can code jobs as well as clients in a Django projectwith minimal overhead in your application. Server connections etc. all takeplace in django-gearman and don't unnecessarily clog your application code.[Gearman]: http://gearman.org[python-gearman]: http://github.com/samuel/python-gearmanInstallation------------It's the same for both the client and worker instances of your django project:pip install -e git://github.com/fwenzel/django-gearman.git#egg=django-gearmanAdd ``django_gearman`` to the `INSTALLED_APPS` section of `settings.py`.Specify the following setting in your local settings.py file:# One or more gearman serversGEARMAN_SERVERS = ['127.0.0.1']Workers-------### Registering jobsCreate a file `gearman_jobs.py` in any of your django apps, and define as manyjobs as functions as you like. The jobs must accept a single argument aspassed by the caller and must return the result of the operation, ifapplicable. (Note: It must accept an argument, even if you don't use it).Mark each of these functions as gearman jobs by decorating them with`django_gearman.decorators.gearman_job`.For an example, look at the `gearman_example` app's `gearman_jobs.py` file.### Job namingThe tasks are given a default name of their import path, with the phrase'gearman_jobs' stripped out of them, for readability reasons. You can overridethe task name by specifying `name` parameter of the decorator. Here's how:@gearman_job(name='my-task-name')def my_task_function(foo):pass### Gearman-internal job naming: ``GEARMAN_JOB_NAME``The setting ``GEARMAN_JOB_NAME`` is a function which takes the original taskname as an argument and returns the gearman-internal version of that taskname. This allows you to map easily usable names in your application to morecomplex, unique ones inside gearman.The default behavior of this method is as follows:new_task_name = '%s.%s' % (crc32(getcwd()), task_name)This way several instances of the same application can be run on the sameserver. You may want to change it if you have several, independent instancesof the same application run against a shared gearman server.If you would like to change this behavior, simply define the``GEARMAN_JOB_NAME`` function in the ``settings.py``:GEARMAN_JOB_NAME = lambda name: namewhich would leave the internal task name unchanged.### Task parametersThe gearman docs specify that the job function can accept only one parameter(usually refered to as the ``data`` parameter). Additionally, that parametermay only be a string. Sometimes that may not be enough. What if you would liketo pass an array or a dict? You would need to serialize and deserialize them.Fortunately, django-gearman can take care of this, so that you can spendall of your time on coding the actual task.@gearman_job(name='my-task-name')def my_task_function(foo):passclient.submit_job('my-task-name', {'foo': 'becomes', 'this': 'dict'})client.submit_job('my-task-name', Decimal(1.0))### Tasks with more than one parameterYou can pass as many arguments as you want, of whatever (serializable) typeyou like. Here's an example job definition:@gearman_job(name='my-task-name')def my_task_function(one, two, three):passYou can execute this function in two different ways:client.submit_job('my-task-name', one=1, two=2, three=3)client.submit_job('my-task-name', args=[1, 2, 3])Unfortunately, executing it like this:client.submit_job('my-task-name', 1, 2, 3)would produce the error, because ``submit_job`` from Gearman's Python bindingscontains __a lot__ of arguments and it's much easier to specify them viakeyword names or a special ``args`` keyword than to type something like seven``None``s instead:client.submit_job('my-task-name', None, None, None, None, None, None, None, 1, 2, 3)The only limitation that you have are gearman reserved keyword parameters. As ofGearman 2.0.2 these are:* data* unique* priority* background* wait_until_complete* max_retries* poll_timeoutSo, if you want your job definition to have, for example, ``unique`` or``background`` keyword parameters, you need to execute the job in a special,more verbose way. Here's an example of such a job and its execution.@gearman_job(name='my-task-name')def my_task_function(background, unique):passclient.submit_job('my-task-name', kwargs={"background": True, "unique": False})client.submit_job('my-task-name', args=[True, False])Finally:client.submit_job('my-task-name', background=True, unique=True, kwargs={"background": False, "unique": False})Don't panic, your task is safe! That's because you're using ``kwargs``directly. Therefore, Gearman's bindings would receive ``True`` for``submit_job`` function, while your task would receive ``False``.Always remember to double-check your parameter names with the reserved wordslist.### Starting a workerTo start a worker, run `python manage.py gearman_worker`. It will startserving all registered jobs.To spawn more than one worker (if, e.g., most of your jobs are I/O bound),use the `-w` option:python manage.py gearman_worker -w 5will start five workers.Since the process will keep running while waiting for and executing jobs,you probably want to run this in a _screen_ session or similar.### Task queuesQueues are a virtual abstraction layer built on top of gearman tasks. Aneasy way to describe it is the following example: Imagine you have a taskfor fetching e-mails from the server, another task for sending the emailsand one more task for sending SMS via an SMS gateway. A problem you mayencounter is that the email fetching tasks may effectively "block" the worker(there could be so many of them, it could be so time-consuming, that no othertask would be able to pass through). Of course, one solution would be to addmore workers (via the ``-w`` parameter), but that would only temporarilysolve the problem. This is where queues come in.The first thing to do is to pass a queue name into the job description, likethis:@gearman_job(queue="my-queue-name")def some_job(some_arg):passYou may then proceed to starting the worker that is bound to the specificqueue:python manage.py gearman_worker -w 5 -q my-queue-nameBe aware of the fact that when you don't specify the queue name, the workerwill take care of all tasks.Clients-------To make your workers work, you need a client app passing data to them. Createand instance of the `django_gearman.GearmanClient` class and execute a`django_gearman.Task` with it:from django_gearman import GearmanClient, Taskclient = GearmanClient()res = client.do_task(Task("gearman_example.reverse", sentence))print "Result: '%s'" % resThe notation for the task name is `appname.jobname`, no matter what patternyou have defined in `GEARMAN_JOB_NAME`.Dispatching a background event without waiting for the result is easy as well:client.dispatch_background_task('gearman_example.background_counting', None)For a live example look at the `gearman_example` app, in the`management/commands/gearman_example_client.py` file.Example App-----------For a full, working, example application, add `gearman_example` to your`INSTALLED_APPS`, then run a worker in one shell:python manage.py gearman_worker -w 4and execute the example app in another:python manage.py gearman_example_clientYou can see the client sending data and the worker(s) working on it.Licensing---------This software is licensed under the [Mozilla Tri-License][MPL]:***** BEGIN LICENSE BLOCK *****Version: MPL 1.1/GPL 2.0/LGPL 2.1The contents of this file are subject to the Mozilla Public License Version1.1 (the "License"); you may not use this file except in compliance withthe License. You may obtain a copy of the License athttp://www.mozilla.org/MPL/Software distributed under the License is distributed on an "AS IS" basis,WITHOUT WARRANTY OF ANY KIND, either express or implied. See the Licensefor the specific language governing rights and limitations under theLicense.The Original Code is django-gearman.The Initial Developer of the Original Code is Mozilla.Portions created by the Initial Developer are Copyright (C) 2010the Initial Developer. All Rights Reserved.Contributor(s):Frederic Wenzel <fwenzel@mozilla.com>Alternatively, the contents of this file may be used under the terms ofeither the GNU General Public License Version 2 or later (the "GPL"), orthe GNU Lesser General Public License Version 2.1 or later (the "LGPL"),in which case the provisions of the GPL or the LGPL are applicable insteadof those above. If you wish to allow use of your version of this file onlyunder the terms of either the GPL or the LGPL, and not to allow others touse your version of this file under the terms of the MPL, indicate yourdecision by deleting the provisions above and replace them with the noticeand other provisions required by the GPL or the LGPL. If you do not deletethe provisions above, a recipient may use your version of this file underthe terms of any one of the MPL, the GPL or the LGPL.***** END LICENSE BLOCK *****[MPL]: http://www.mozilla.org/MPL/