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, 2019 in Python by sumanth
• 190 points
1,853 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, 2019 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
652 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 24, 2018 in Python by Aryya
• 500 points
511 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, 2019 in Python by Nymeria
• 3,560 points
982 views
0 votes
2 answers
+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 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,442 views
0 votes
1 answer
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
4,078 views
+1 vote
1 answer
0 votes
1 answer

How do I create a dataframe using a dictionary in pandas?

Hi @Hannah, You need to define your dictionary ...READ MORE

answered Nov 18, 2019 in Python by Eric
783 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP