There are different places you can add the try block. It all depends on where you think you might find an exception. Here are few examples:
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():
try:
fire_and_forget(url, json=data, headers=headers)
except:
print ("Thread creation failure")
or something like this:
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):
try:
x=threading.Thread(target=request_task, args=(url, json, headers))
except:
print ("Thread creation failure")
try:
x.start()
except:
print("Could not start thread")
def push():
fire_and_forget(url, json=data, headers=headers)