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.

4

2 回答 2

3

reversed()返回一个迭代器,而不是一个字节串。改用负步幅切片技巧:

return gearman_job.data[::-1]

这将返回一个反转的字符串。

相比:

>>> reversed('somedata')
<reversed object at 0x100480e50>
>>> 'somedata'[::-1]
'atademos'
于 2012-10-31T12:02:37.457 回答
0

为了其他人面临类似错误,您需要从 worker 返回一个字符串。如果你没有显式返回或返回任何其他类型的数据,scrapy 会抛出错误。原因很简单,Gearman 的协议是基于文本的。

于 2015-10-08T14:58:11.740 回答