gpt4 book ai didi

python - django Thread.start() 不工作 thread.is_alive() 返回 false

转载 作者:太空宇宙 更新时间:2023-11-03 17:41:36 25 4
gpt4 key购买 nike

如标题中所述,我的线程无法正常工作。有关更多详细信息,请查看下面的代码:

回调中调用的线程

def callback(request):
response_data = {}
code = request.GET.get('code', None)
if code:
response_data['code']= code
thr = threading.Thread(target=request_token_Api, args=(request, code, response_data))
thr.setDaemon(True)
thr.start()
value =thr.is_alive()
print("Thread is Alive:",value)
thr.join()
return JsonResponse(response_data)

我调用的方法 (request_token_Api) 进行 Oauth2 token 请求调用,但是当我检查 thred.is_alive() 时,我在返回中得到 False这应该是真的,我不知道为什么。我尝试在 thread.start 之后添加 thread.run(),但收到 AttributeError 消息。

request_token_Api 方法

def request_token_Api(request, code, response_data):
if request.method =='POST':
url = "https://oauth2.api_token_request.com"
params = {
'grant_type': 'authorization_code',
'client_id': API_KEY,
'client_secret': SECRET_KEY,
'code': code,
'redirect_uri': URI
}
response = requests.get(url, params=params, verify=True)
reponse= response.json()
return reponse

最佳答案

我怀疑request_token_Api在您有机会检查之前就退出了,因此线程已经完成。在 request_token_Api仅当方法为“POST”时才执行某些操作,但在 callback 中您正在查看GET request的属性(property)并确保你在那里有值(value)。所以这两件事可能永远不会都是 True

您应该更改何时调用 request_token_Api 的逻辑或者当你身处其中时你期望持有什么逻辑。但如果您有有效的 GET代码,您可能没有带着 POST 到达那里方法。

关于python - django Thread.start() 不工作 thread.is_alive() 返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30465966/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com