我有以下'worker'最初返回一个JSON对象,但我希望它返回多个JSON对象:
def data_worker(data):
_cats, index, total = data
_breeds = {}
try:
url = _channels['feedUrl']
r = get(url, timeout=5)
rss = etree.XML(r.content)
tags = rss.xpath('//cats/item')
_cats['breeds'] = {}
for t in tags:
_cats['breeds']["".join(t.xpath('breed/@url'))] = True
_breeds['url'] = "".join(t.xpath('breed/@url'))
return [_cats, _breeds]
except:
return [_cats, _breeds]
此worker是多处理池的参数:
cats, breeds = pool.map(data_worker, data, chunksize=1)
当我只使用一个输出(即_cats)运行池和工作程序时,它工作得很好,但是当我尝试返回多个JSON“模式”时,我收到错误:
File "crawl.py", line 111, in addFeedData
[cats, breeds] = pool.map(data_worker, data, chunksize=1)
ValueError: too many values to unpack
如何在data_worker中返回2个单独的JSON对象?我需要将它们作为单独的JSON对象。请注意,我已经尝试过以下操作,但这些操作无效:
[cats, breeds] = pool.map(data_worker, data, chunksize=1)
(cats, breeds) = pool.map(data_worker, data, chunksize=1)
return (_cats, _breeds)
答案 0 :(得分:1)
首先,我认为你打算写这个:
cats, breeds = pool.map(data_worker, data, chunksize=1)
但无论如何这不会起作用,因为data_worker
会返回一对,但map()
会返回一个工作人员返回的列表。所以你应该这样做:
cats = []
breeds = []
for cat, breed in pool.map(data_worker, data, chunksize=1):
cats.append(cat)
breeds.append(breed)
这将为您提供您寻找的两个列表。
换句话说,你期望一对列表,但你得到了一对列表。