Skip to content

Commit

Permalink
docs: add DatabaseTask example
Browse files Browse the repository at this point in the history
  • Loading branch information
cenkalti committed Aug 3, 2013
1 parent 9793d76 commit 791bb03
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions docs/extending.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,36 @@ Extending Task Class
decorator accept an optional ``task_class`` argument.
You can pass a subclass of :class:`~kuyruk.Task` to change it's behavior.

Example:

.. code-block:: python
from kuyruk import Kuyruk, Task
from myapp.orm import Session
kuyruk = Kuyruk()
class DatabaseTask(Task):
"""Open a new database session before running tasks
and close on finish."""
def setup(self):
self.connect_signal(task_prerun, self.open_session)
self.connect_signal(task_postrun, self.close_session)
def open_session(self, *args, **kwargs):
self.session = Session()
def close_session(self, *args, **kwargs):
self.session.close()
@kuyruk.task(task_class=DatabaseTask)
def a_task_with_a_session(some_arg):
session = self.session
# Work with the session...
session.commit()
Extending Worker Class
----------------------
Expand Down

0 comments on commit 791bb03

Please sign in to comment.