1

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.

ducky
  • 1,113
  • 1
  • 11
  • 23

2 Answers2

3

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'
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
0

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.

Mayank Jaiswal
  • 12,338
  • 7
  • 39
  • 41