繁体   English   中英

使用线程时处理请求异常?

[英]Handling request exceptions while using threads?

我在线程中使用 request.post,因为我已经击中了 url 并且不等待响应,因为我有大约 4.5 k 事件一次发送一个事件。

def request_task(url, data, headers):
    try:
        response = requests.post(url, json=data, headers=headers)

    except requests.ConnectionError as e:
        print (e)
     except requests.exceptions.RequestException as e:
         print (e)

def fire_and_forget(url, json, headers):
    x=threading.Thread(target=request_task, args=(url, json, headers))
    x.start()

def push():
    fire_and_forget(url, json=data, headers=headers)

我不确定我应该在哪里使用 try 块,我是否以正确的方式使用线程。

任何人都可以建议我如何在考虑线程的情况下捕获异常

在使用 request.post 我们可以使用

def request_task(url, data, headers):
try:
    r = requests.post(url, json=data, headers=headers)
    r.raise_for_status()
except requests.exceptions.HTTPError as e:
    print (e.response.text)

捕获异常。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM