Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
django并发请求两个url,每个请求头对每个url都是错误的_Django - Fatal编程技术网

django并发请求两个url,每个请求头对每个url都是错误的

django并发请求两个url,每个请求头对每个url都是错误的,django,Django,用两个url并发请求django应用程序,每个url请求头我给出一个id,比如urla id=1,urlb id=2,但当我从请求头的django视图中获取时,我从请求头中获取的urla id是2…太混乱了…帖子正文是可以的 客户端代码 #coding=utf-8 import threadpool import requests HOST = 'http://127.0.0.1:8000' urls = ['/health', '/x2'] headers = {'Content-Type

用两个url并发请求django应用程序,每个url请求头我给出一个id,比如urla id=1,urlb id=2,但当我从请求头的django视图中获取时,我从请求头中获取的urla id是2…太混乱了…帖子正文是可以的

客户端代码

#coding=utf-8
import threadpool

import requests

HOST = 'http://127.0.0.1:8000'
urls = ['/health', '/x2']
headers = {'Content-Type': 'application/json', 'HOST': 'xx.com'}

pool = threadpool.ThreadPool(5)

def request_api(n):
    url = urls[n%2]
    print('---------------------start request url {}'.format(url))
    headers.update(url=url)
    body = dict(
        url=url
    )
    r = requests.post(url=HOST + url, headers=headers, json=body)
    print(r.status_code)

reqs = threadpool.makeRequests(request_api, range(5))
[pool.putRequest(req) for req in reqs]
pool.wait()
print('over')
服务器代码

# coding=utf-8
from rest_framework.views import APIView
from django.http.response import HttpResponse


class ApiHealthCheck(APIView):
    def post(self, request, request_id='', **kwargs):
        print('api_health_check: {},{},{}'.format(request.path, request.META.get('HTTP_URL'), request.data))
        return HttpResponse("ok")


class ApiHealthCheck2(APIView):
    def post(self, request, request_id='', **kwargs):
        print('api_health_check2: {},{},{}'.format(request.path, request.META.get('HTTP_URL'), request.data))
        return HttpResponse("ok")
服务器打印

api_health_check: /health,/health,{u'url': u'/health'}
api_health_check2: /x2,/health,{u'url': u'/x2'}
api_health_check: /health,/health,{u'url': u'/health'}
api_health_check2: /x2,/x2,{u'url': u'/x2'}
api_health_check: /health,/health,{u'url': u'/health'}

注意其中一个有错误,路径与META的内容不匹配,正文可以

对不起,我发现了问题,不是django问题,是我的错

headers = {'Content-Type': 'application/json', 'HOST': 'xx.com'}
headers是一个全局变量,并发请求可以并发修改这个头,所以请求django,django得到错误的头