Dragon
Dragon

Reputation: 739

is Python's Http request synchronous?

I am new to Python, I want to understand if the Http request is Synchronous or Async? do i need to implement callbacks ?

I am using urllib2 module and below is the syntax:

content = urllib2.urlopen(urlnew).read()

At my server there are more than 30,000 records and for each one there will be an http call and the response received will be stored.

Any help appreciated.

Upvotes: 5

Views: 18690

Answers (1)

poke
poke

Reputation: 387617

Like most Python stuff, unless explicitely mentioned, urllib2 is synchronous. So the execution will block until the server responded.

So if you want to make 30,000 requests, you will have to do one request after the other one. An alternative would be to launch the requests in multiple processes (using multiprocessing) to parallelize it.

But the better option, especially since you seem to be in control of the server, would be to have it provide some kind of batch request that allows you to query multiple (or all) records at once.

Upvotes: 9

Related Questions