Handling request exceptions while using threads?

0 votes

i am using request.post in thread as i have hit the url and not wait for response as i have around 4.5 k event to be sent one event at a time.

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)

I am not sure where should i be using try block and am i doing it the right way as i am using thread .

Could any one suggest how can i catch the exceptions also taking thread into consideration

Nov 4 in Python by sumanth
• 190 points
44 views

1 answer to this question.

0 votes

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)
answered Nov 4 by Richard
hi @Richard thank you for your answer .
But my concern is i am not able to catch the exception .
example : in resquest_task method if i pass a wrong URL also i am not able to catch the exception.

Hello @sumanth,

I tried the code and it worked without any problem. When I give a wrong url, the exception was caught and was printed on the console:

HTTPConnectionPool(host='www.edurekka.co', port=80): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x000002682CDB5E80>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed',))

Related Questions In Python

0 votes
1 answer

Using exceptions in Python

They are used for control flow. Check ...READ MORE

answered Jun 14, 2018 in Python by Nietzsche's daemon
• 4,260 points
31 views
0 votes
0 answers

How to stop threads spawned by BaseHTTPServer using ThreadingMixing

I read a post somewhere that using ThreadingMixin (from the SocketServer module), ...READ MORE

Oct 23, 2018 in Python by Aryya
• 500 points
20 views
0 votes
1 answer
0 votes
1 answer

How to get textual output when using exceptions in Python?

Hi, the answer is pretty simple.  Without the ...READ MORE

answered Jan 17 in Python by Nymeria
• 3,520 points
41 views
0 votes
0 answers

try except is not working while using hdfs command

Hi,  I am trying to run following things ...READ MORE

Mar 6 in Python by anonymous
54 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 341 views
+4 votes
6 answers
+1 vote
1 answer