Python client

Samuel Stauffer samuel at descolada.com
Thu Jul 12 22:38:34 UTC 2007


I wrote a quick hack of Python client for Gearman, and although it
will be completely rewritten I figure it's worth putting out there
since it doesn't seem that there are any other Python clients
available. The plan is to write a more durable and clean library
without the need for twisted when there's more time. I hope someone
can make some kind of use out of this at least.

http://satel.ethereal.net/gearman.py

Usage is best described by an example:

    hosts = ["127.0.0.1:7003"]

    class TestWorker(object):
        @staticmethod
        def echo(job):
            return job.arg

    # Worker
    worker = GearmanWorker(job_servers=hosts)
    worker.register_class(TestWorker)
    worker.start_working()
    # worker.run()

    # Client
    client = GearmanClient(job_servers=hosts)
    d = client.do_task("__main__.TestWorker.echo", "test", "-",
        on_success=lambda blah:sys.stdout.write("DONE: %s\n" % blah),
        on_status=lambda num,den:sys.stdout.write("STATUS: %s/%s\n" %
(num, den)))
    def _error(exc):
        print "ERROR: %s" % exc.type
    def _created(job):
        print "CREATED: %s" % job.handle
        # job.deferred.addCallbacks(
        #     lambda res:sys.stdout.write("DONE: %s\n" % res),
        #     _error,
        # ),
    d.addCallbacks(_created, _error)
    client.wait_for_jobs()


do_task returns a deferred that gets called when the task gets created
(not when it completes) with the Job class instance for that task.
job.deferred is the actual deferred for the task and gets called when
the task completes. on_success is a shortcut so that you don't need to
have a callback on the task creation that adds a callback for the task
completion. (eeeks!) "task is created" means that the server sent back
a handle for the task.

As I said though, it's all quite messy. heh. My only use so far has
been to play around with Djabberd (which is why it was written) so I
wouldn't consider it tested by any stretch of the imagination.

-
Samuel Stauffer


More information about the Gearman mailing list