Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python gevent&x2B;使用socks时请求阻塞_Python_Python Requests_Gevent_Socks - Fatal编程技术网

Python gevent&x2B;使用socks时请求阻塞

Python gevent&x2B;使用socks时请求阻塞,python,python-requests,gevent,socks,Python,Python Requests,Gevent,Socks,我使用python(2.7.6)、gevnet(1.1.2)、requests(2.11.1)使http请求并发,效果很好。但当我将socks代理添加到请求时,它会阻塞 这是我的代码: import time import requests import logging import click import gevent from gevent import monkey monkey.patch_all() FORMAT =

我使用python(2.7.6)、gevnet(1.1.2)、requests(2.11.1)使http请求并发,效果很好。但当我将socks代理添加到请求时,它会阻塞

这是我的代码:

    import time
    import requests
    import logging
    import click

    import gevent
    from gevent import monkey
    monkey.patch_all()

    FORMAT = '%(asctime)-15s %(message)s'
    logging.basicConfig(format=FORMAT)
    logger = logging.getLogger('test')

    #socks proxy
    user = MY_SOCKS_PROXY_USERNAME
    password = MY_SOCKS_PROXY_PASSWORD
    host = MY_SOCKS_PROXY_HOST
    port = MY_SOCKS_PROXY_PORT
    proxies = {
            'http': 'socks5://{0}:{1}@{2}:{3}'.format(user, password, host, port),
            'https': 'socks5://{0}:{1}@{2}:{3}'.format(user, password, host, port),
    }
    url = 'https://www.youtube.com/user/NBA'


    def fetch_url(i,with_proxy):
        while True:
            logger.warning('thread %s fetch url'%i)
            try:
                if with_proxy:
                    res = requests.get(url,proxies=proxies, timeout=5)
                else:
                    res = requests.get(url, timeout=5)
            except Exception as e:
                logger.error(str(e))
                continue

            logger.warning(res.status_code)

    def do_other_thing():
        while True:
            logger.warning('do other thing...')
            time.sleep(1)

    @click.command()
    @click.option('--with_proxy/--without_proxy',help='if use proxy', default=True)
    def run(with_proxy):
        if with_proxy:
            logger.warning('with proxy......')
        else:
            logger.warning('without proxy......')
        ts = []
        ts.append(gevent.spawn(do_other_thing))
        for i in xrange(3):
            ts.append(gevent.spawn(fetch_url,i,with_proxy))
        gevent.joinall(ts)

    if __name__=='__main__':
        run()
这些图片显示了结果

使用代理,在获取url之前,do_其他东西将被阻止

如果没有代理,它工作得很好。(由于GFW,出现超时错误)


有人能帮我解决这个问题吗?非常感谢

我在github也问了这个问题。合作者很好,帮我解决这个问题。解决方案非常简单

在执行任何其他操作之前,将gevent导入和monkeypatch移动到文件的最顶端

我还有一个包含多个文件的项目,将import和monkeypatch移动到第一个文件的顶部可以解决我的问题


你试过了吗?@Maurice Meyer。不,我没有。因为grequests不能满足我项目的要求。还是谢谢你!