File: 1to2.rst

package info (click to toggle)
python-gearman 2.0.2-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch, wheezy
  • size: 464 kB
  • ctags: 471
  • sloc: python: 2,285; makefile: 135
file content (91 lines) | stat: -rw-r--r-- 2,926 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
==============================================
Transitioning from python-gearman 1.x to 2.0.0
==============================================

Client (single task)
====================
::

    # python-gearman 1.x
    old_client = gearman.GearmanClient(['localhost:4730'])
    old_result = old_client.do_task(Task("echo", "foo"))


    # python-gearman 2.x
    new_client = gearman.GearmanClient(['localhost:4730'])
    current_request = new_client.submit_job('echo', 'foo')
    new_result = current_request.result

Client (multiple tasks)
=======================
::

    # python-gearman 1.x
    old_client = gearman.GearmanClient(['localhost:4730'])
    ts = Taskset([
        Task(func="echo", arg="foo"),
        Task(func="echo", arg="bar"),
    ])
    old_client.do_taskset(ts)
    for task in ts.values():
        assert task.result == task.arg


    # python-gearman 2.x
    new_client = gearman.GearmanClient(['localhost:4730'])
    new_jobs = [
        dict(task='echo', data='foo'),
        dict(task='echo', data='bar'),
    ]

    completed_requests = new_client.submit_multiple_jobs(new_jobs)
    for current_request in completed_requests:
        assert current_request.result == current_request.job.data

Worker
======
::

    # python-gearman 1.x
    class WorkerHook(object):
        def start(self, current_job):
            print "Job started"

        def fail(self, current_job, exc_info):
            print "Job failed, can't stop last gasp GEARMAN_COMMAND_WORK_FAIL"

        def complete(self, current_job, result):
            print "Job complete, can't stop last gasp GEARMAN_COMMAND_WORK_COMPLETE"

    def callback_fxn(idle, last_job_time):
        return False

    old_worker = gearman.GearmanWorker(['localhost:4730'])
    old_worker.register_function("echo", lambda job:job.arg)
    old_worker.work(stop_if=callback_fxn, hooks=WorkerHook())


    # python-gearman 2.x
    class CustomGearmanWorker(gearman.GearmanWorker):
        def on_job_execute(self, current_job):
            print "Job started"
            return super(CustomGearmanWorker, self).on_job_execute(current_job)

        def on_job_exception(self, current_job, exc_info):
            print "Job failed, CAN stop last gasp GEARMAN_COMMAND_WORK_FAIL"
            return super(CustomGearmanWorker, self).on_job_exception(current_job, exc_info)

        def on_job_complete(self, current_job, job_result):
            print "Job failed, CAN stop last gasp GEARMAN_COMMAND_WORK_FAIL"
            return super(CustomGearmanWorker, self).send_job_complete(current_job, job_result)

        def after_poll(self, any_activity):
            # Return True if you want to continue polling, replaces callback_fxn
            return True

    def task_callback(gearman_worker, job):
        return job.data

    new_worker = CustomGearmanWorker(['localhost:4730'])
    new_worker.register_task("echo", task_callback)
    new_worker.work()