我用ajax调用这个函数。主要目的是返回带有一些数据的HttpResponse对象。数据取决于登录结果。
def validate_account 返回布尔值。如何在python中的proccess中设置 is_valid varraible?
process.join()是还原剂?
@login_required
@csrf_protect
def create_account(request):
if request.user.is_authenticated():
username = ''
password = ''
body_unicode = request.body.decode('utf-8')
body = json.loads(body_unicode)
try:
username = body['username']
password = body['password']
except KeyError as e:
log.exception(e)
return HttpResponseServerError()
try:
accounts_of_user = Account.objects.get(username=username)
if accounts_of_user:
return HttpResponse('901', content_type='text/plain')
except ObjectDoesNotExist as e:
is_valid = False
def new_process():
is_valid = validate_account(username=username, password=password)
process = Process(target=new_process())
process.daemon = True
process.start()
process.join()
process.terminate()
print(is_valid)
if is_valid:
signer = Signer(salt='extra')
account = Account()
account.username = username
account.password = signer.sign(password)
account.save()
request.session['current_account'] = username
return HttpResponse('902', content_type='text/plain')
else:
return HttpResponse('903', content_type='text/plain')
else:
return HttpResponseForbidden()