def_user_batch_import(self, q, account_id):from flask importrequestfrom app.api.apis importimport_user_phone, import_user_birthdate
failure_list= list() #所有错误结果的数据
failure_dict = dict() #unique_id错误的字典
success = list() #所有正确结果的数据
wrong_format = list() #所有格式错误的数据
res_dic = dict() #return返回的数据
#headers_token = request.headers["Authorization"] if 'Authorization' in request.headers and len(
#request.headers["Authorization"].split(" ")) == 2 else ''
headers_token =AccessToken
header= {"Authorization": "Bearer %s" %headers_token}
request_query=[]while q.qsize() >0:
query=q.get()#res_phone = impot_user_phone(query["pseudonym[unique_id]"])
res_phone = import_user_phone(query["pseudonym[unique_id]"])
res_birthdate= import_user_birthdate(query["user[birthdate]"])#res_password = impot_user_password(query["pseudonym[password]"])
#res_id_card = import_user_id_card(query["user[id_card]"])
#if res_id_card in ["", None]:
#res_user_id_card_num = 0
#else:
#res_user_id_card_num = User.query.filter(User.id_card == query["user[id_card]"], User.workflow_state != "deleted").count()
#if isinstance(res_phone, dict) or isinstance(res_password, dict) or isinstance(res_id_card, dict) or res_user_id_card_num != 0:
#if isinstance(res_phone, dict) or isinstance(res_id_card, dict) or res_user_id_card_num != 0:
if isinstance(res_phone, dict) orisinstance(res_birthdate, dict):
wrong_format.append(query["pseudonym[unique_id]"])
query["errcode"] = 1
if "errcode" not inquery.keys():
request_query.append(query)#将loop对象设置为全局对象
loop =asyncio.new_event_loop()
asyncio.set_event_loop(loop)
canvas_url= API_CANVAS_HOST + '/api/v1/accounts/{account_id}/users'.format(account_id=account_id)
results=loop.run_until_complete(self.url_res(canvas_url, header, request_query))#关闭loop对象
loop.close()print("results----------------")print(results)for result inresults:print(result)print(result.json())if result.status_code == 403:return {"errcode": 400, "errmsg": u"无此权限"}
res_json=result.json()if "errors" inres_json.keys():try:
failure_dict["phone"] = res_json["errors"]["unique_id"]
failure_dict["message"] = res_json["errors"]["pseudonym"]["unique_id"][0]["message"]
failure_list.append(failure_dict.copy())exceptException as e:
failure_list.append({"canvas_errors": res_json, "errmsg_e": str(e)})else:
success.append(res_json["login_id"])
res_dic["wrong_format"] =wrong_format
res_dic["success_event"] =success
res_dic["failure_event"] =failure_listreturn {"errcode": 0, "errmsg": "ok", "data": res_dic}#if len(res_dic["wrong_format"]) == 0 and len(res_dic["failure_event"]) == 0:
#return {"errcode": 0, "errmsg": "ok"}
#else:
#return {"errcode": 400, "errmsg": "failure", "data": res_dic}
asyncdefdo_some_work(self, url, header, query):return requests.post(url, headers=header, data=query)
asyncdefurl_res(self, url, header, request_query):
tasks=[]for query inrequest_query:
coroutine=self.do_some_work(url, header, query)
temp=asyncio.ensure_future(coroutine)
tasks.append(temp)return await asyncio.gather(*tasks)