Error running basic python-gearman example

947 views Asked by At

I am trying to run a basic example of gearman using python-gearman library available here. I am running python 2.7.3

Worker:

import gearman

gm_worker = gearman.GearmanWorker(['localhost:4730'])

def task_listener_reverse(gearman_worker, gearman_job):
    print 'reporting status'
    return reversed(gearman_job.data)

gm_worker.set_client_id('testclient')
gm_worker.register_task('reverse', task_listener_reverse)
gm_worker.work()

Client:

import gearman

gm_client = gearman.GearmanClient(['localhost:4730'])
print 'Sending job...'
request = gm_client.submit_job('reverse', 'Hello World!')
print "Result: " + request.result

I am getting the following error (full trace available here)

File "/Users/developer/gearman/connection_manager.py", line 27, in _enforce_byte_string
    raise TypeError("Expecting byte string, got %r" % type(given_object))
TypeError: Expecting byte string, got <type 'reversed'>

Any help would be appreciated!

Thanks.

2

There are 2 answers

0
Martijn Pieters On BEST ANSWER

reversed() returns an iterator, not a bytestring. Use the negative stride slicing trick instead:

return gearman_job.data[::-1]

This returns a reversed string instead.

Compare:

>>> reversed('somedata')
<reversed object at 0x100480e50>
>>> 'somedata'[::-1]
'atademos'
0
Mayank Jaiswal On

For the sake of other people facing similar errors, you need to return a string from worker. If you do not return explicitly or return data of any other type, scrapy throws an error. Reason is simple that Gearman's protocol is text based.