Python requests module multithreading

后端 未结 2 1946
旧时难觅i
旧时难觅i 2021-01-07 14:08

Is there a possible way to speed up my code using multiprocessing interface? The problem is that this interface uses map function, which works only with 1 function. But my c

2条回答
  •  一整个雨季
    2021-01-07 14:58

    There are many ways to accomplish your task, but multiprocessing is not needed at that level, it will just add complexity, imho.

    Take a look at gevent, greenlets and monkey patching, instead!

    Once your code is ready, you can wrap a main function into a gevent loop, and if you applied the monkey patches, the gevent framework will run N jobs concurrently (you can create a jobs pool, set the limits of concurrency, etc.)

    This example should help:

    #!/usr/bin/python
    # Copyright (c) 2009 Denis Bilenko. See LICENSE for details.
    
    """Spawn multiple workers and wait for them to complete"""
    from __future__ import print_function
    import sys
    
    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()
    
    
    if sys.version_info[0] == 3:
        from urllib.request import urlopen
    else:
        from urllib2 import urlopen
    
    
    def print_head(url):
        print('Starting %s' % url)
        data = urlopen(url).read()
        print('%s: %s bytes: %r' % (url, len(data), data[:50]))
    
    jobs = [gevent.spawn(print_head, url) for url in urls]
    
    gevent.wait(jobs)
    

    You can find more here and in the Github repository, from where this example comes from

    P.S. Greenlets will works with requests as well, you don't need to change your code.

提交回复
热议问题