Multiple (asynchronous) connections with urllib2 or other http library?
Asked Answered
C

6

14

I have code like this.

for p in range(1,1000):
    result = False
    while result is False:
        ret = urllib2.Request('http://server/?'+str(p))
        try:
            result = process(urllib2.urlopen(ret).read())
        except (urllib2.HTTPError, urllib2.URLError):
            pass
    results.append(result)

I would like to make two or three request at the same time to accelerate this. Can I use urllib2 for this, and how? If not which other library should I use? Thanks.

Cripple answered 7/11, 2010 at 21:4 Comment(0)
H
11

So, it's 2016 πŸ˜‰ and we have Python 3.4+ with built-in asyncio module for asynchronous I/O. We can use aiohttp as HTTP client to download multiple URLs in parallel.

import asyncio
from aiohttp import ClientSession

async def fetch(url):
    async with ClientSession() as session:
        async with session.get(url) as response:
            return await response.read()

async def run(loop, r):
    url = "http://localhost:8080/{}"
    tasks = []
    for i in range(r):
        task = asyncio.ensure_future(fetch(url.format(i)))
        tasks.append(task)

    responses = await asyncio.gather(*tasks)
    # you now have all response bodies in this variable
    print(responses)

loop = asyncio.get_event_loop()
future = asyncio.ensure_future(run(loop, 4))
loop.run_until_complete(future)

Source: copy-pasted from http://pawelmhm.github.io/asyncio/python/aiohttp/2016/04/22/asyncio-aiohttp.html

Hardesty answered 27/7, 2016 at 20:8 Comment(0)
S
10

You can use asynchronous IO to do this.

requests + gevent = grequests

GRequests allows you to use Requests with Gevent to make asynchronous HTTP Requests easily.

import grequests

urls = [
    'http://www.heroku.com',
    'http://tablib.org',
    'http://httpbin.org',
    'http://python-requests.org',
    'http://kennethreitz.com'
]

rs = (grequests.get(u) for u in urls)
grequests.map(rs)
Saccharo answered 28/9, 2012 at 9:30 Comment(2)
Could you detail how to pass a function to process the response ? Docs don't seem to mention it – Strega
@Strega You can use docs.python-requests.org/en/master/user/advanced/#event-hooks example: grequests.get(u, hooks=dict(response=print_url)) or you can use grequests.get(u, callback=print_url) – Responsum
F
9

Take a look at gevent β€” a coroutine-based Python networking library that uses greenlet to provide a high-level synchronous API on top of libevent event loop.

Example:

#!/usr/bin/python
# Copyright (c) 2009 Denis Bilenko. See LICENSE for details.

"""Spawn multiple workers and wait for them to complete"""

urls = ['http://www.google.com', 'http://www.yandex.ru', 'http://www.python.org']

import gevent
from gevent import monkey

# patches stdlib (including socket and ssl modules) to cooperate with other greenlets
monkey.patch_all()

import urllib2


def print_head(url):
    print 'Starting %s' % url
    data = urllib2.urlopen(url).read()
    print '%s: %s bytes: %r' % (url, len(data), data[:50])

jobs = [gevent.spawn(print_head, url) for url in urls]

gevent.joinall(jobs)
Frizz answered 7/11, 2010 at 22:33 Comment(0)
C
2

2021 answer using modern async libraries

The 2016 answer is good, but i figured i'd throw in another answer with httpx instead of aiohttp, since httpx is only a client and supports different async environments. I'm leaving out the OP's for loop with urls built from a number concatenated to the string for what i feel is a more generic answer.

import asyncio
import httpx

# you can have synchronous code here

async def getURL(url):
    async with httpx.AsyncClient() as client:
        response = await client.get(url)
        # we could have some synchronous code here too
        # to do CPU bound tasks on what we just fetched for instance
        return response

# more synchronous code can go here

async def main():
    response1, response2 = await asyncio.gather(getURL(url1),getURL(url2))
    # do things with the responses
    # you can also have synchronous code here

asyncio.run(main()) 

Code after any await within the async with block will run as soon as the awaited task is done. It is a good spot to parse your response without waiting for all your requests to have completed.

Code after the asyncio.gather will run once all the tasks have completed. It is a good place to do operations requiring information from all the requests, possibly pre-processed in the async function called by gather.

Casemate answered 20/10, 2021 at 9:10 Comment(0)
A
1

maybe using multiprocessing and divide you work on 2 process or so .

Here is an example (it's not tested)

import multiprocessing
import Queue
import urllib2


NUM_PROCESS = 2
NUM_URL = 1000


class DownloadProcess(multiprocessing.Process):
    """Download Process """

    def __init__(self, urls_queue, result_queue):

        multiprocessing.Process.__init__(self)

        self.urls = urls_queue
        self.result = result_queue

    def run(self):
        while True:

             try:
                 url = self.urls.get_nowait()
             except Queue.Empty:
                 break

             ret = urllib2.Request(url)
             res = urllib2.urlopen(ret)

             try:
                 result = res.read()
             except (urllib2.HTTPError, urllib2.URLError):
                     pass

             self.result.put(result)


def main():

    main_url = 'http://server/?%s'

    urls_queue = multiprocessing.Queue()
    for p in range(1, NUM_URL):
        urls_queue.put(main_url % p)

    result_queue = multiprocessing.Queue()

    for i in range(NUM_PROCESS):
        download = DownloadProcess(urls_queue, result_queue)
        download.start()

    results = []
    while result_queue:
        result = result_queue.get()
        results.append(result)

    return results

if __name__ == "__main__":
    results = main()

    for res in results:
        print(res)
Anagoge answered 7/11, 2010 at 22:49 Comment(1)
Threading is the right answer, not complex layered things like Twisted. I'd use threading rather than multiprocessing; the process-based multiprocessing module is only needed for CPU-bound tasks, not this I/O-bound one. – Aikido
C
1

I know this question is a little old, but I thought it might be useful to promote another async solution built on the requests library.

list_of_requests = ['http://moop.com', 'http://doop.com', ...]

from simple_requests import Requests
for response in Requests().swarm(list_of_requests):
    print response.content

The docs are here: http://pythonhosted.org/simple-requests/

Complaint answered 21/10, 2013 at 15:22 Comment(0)

© 2022 - 2024 β€” McMap. All rights reserved.