Python "requests" not working / blocking in a thread?

Viewed 1729

I've tried the requests module in python 2.7 and liked it a lot, but when I tried to use it in a thread, it doesn't seem to work:

def doWork():
    try:
        print "before"
        requests.get('http://www.google.fr')
        print "after"
    except Exception as e:
        print "Error : "+ str(e)

# Working : I see "before" & "after"
doWork()

# Not working, i see "before" but never "after"
t = Thread(target=doWork)
t.start()

I it tried this way but it's the same:

class TestThread(Thread):
    def run(self):
        try:
            print "before"
            requests.get('http://www.google.fr')
            print "after"
        except Exception as e:
            print "Error : "+ str(e)

test_thread = TestThread()
test_thread.start() # I will see "before" but never "after"
test_thread.join()

I tried to wait few minutes (hours), but it is still not working; neither after nor an error is ever printed. I probably missed something but I can't see what.

I am using Requests 2.9.1; Python 2.7.11 (Anaconda 2.4.1 32bits) and I'm on Windows 7.

1 Answers
Related