此表单的目的是让用户进入很多地方(逗号分隔),它将检索手机,名称,网站。让它在python IDE中工作,没问题,但是在将它放入我的webapp中时会出现问题。
我在分配Exception Value: Can't pickle local object 'GetNums.<locals>.get_data'
的行收到错误a
。我检查了inputText
的类型,并确认它确实是一个列表。所以,我不确定为什么它不会腌制。
def GetNums(request):
form = GetNumsForm(request.POST or None)
if form.is_valid():
inputText = form.cleaned_data.get('getnums')
# all experimental
inputText = inputText.split(',')
def get_data(i):
#DON'T FORGET TO MOVE THE PRIMARY KEY LATER TO SETTINGS
r1 = requests.get('https://maps.googleapis.com/maps/api/place/textsearch/json?query=' + i + '&key=GET_YOUR_OWN')
a = r1.json()
pid = a['results'][0]['place_id']
r2 = requests.get('https://maps.googleapis.com/maps/api/place/details/json?placeid=' + pid + '&key=GET_YOUR_OWN')
b = r2.json()
phone = b['result']['formatted_phone_number']
name = b['result']['name']
try:
website = b['result']['website']
except:
website ='No website found'
return ' '.join((phone, name, website))
v = str(type(inputText))
with Pool(5) as p:
a = (p.map(get_data, inputText))
# for line in p.map(get_data, inputText):
# print(line)
#code assist by http://stackoverflow.com/a/34512870/5037442
#end experimental
return render(request, 'about.html', {'v': a})
答案 0 :(得分:2)
在尝试挑选get_data
时,它实际上正在bar,这是一个嵌套的函数/闭包。
将get_data
移出GetNums
(并请将其重命名为snake_case),它应该有效。