【问题标题】:python requests-futures slow - not threading properly?python requests-futures 慢 - 线程不正确?
【发布时间】:2014-08-17 14:37:33
【问题描述】:

您好,我使用 requests-futures 库编写了一个多线程请求和响应处理程序。

但是,它似乎很慢,而且不像我想象的那样异步。输出缓慢且有序,如果线程正确,则不会像我预期的那样交错。

我的问题是为什么我的代码很慢,我该怎么做才能加快速度?举个例子就好了。

代码如下:

#!/usr/bin/python
import requests
import time
from concurrent.futures import ThreadPoolExecutor
from requests_futures.sessions import FuturesSession

session = FuturesSession(executor=ThreadPoolExecutor(max_workers=12))

def responseCallback( sess, resp ):
    response = resp.text
    if not "things are invalid" in response in response:
        resp.data = "SUCCESS %s" % resp.headers['content-length']
    else:
        resp.data = "FAIL %s" % resp.headers['content-length']

proxies = {
"http":"http://localhost:8080",
"https":"https://localhost:8080"
}

url = 'https://www.examplehere.com/blah/etc/'
headers= {
'Host':'www.examplehere.com',
'Connection':'close',
'Cache-Control':'max-age=0',
'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Origin':'https://www.examplehere.com',
'User-Agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/533.32 (KHTML, like Gecko) Ubuntu Chromium/34.0.1847.123 Chrome/34.0.1847.123 Safari/337.12',
'Content-Type':'application/x-www-form-urlencoded',
'Referer':'https://www.exampleblah.etc/',
'Accept-Encoding':'gzip,deflate,sdch',
'Accept-Language':'en-US,en;q=0.8,de;q=0.6',
'Cookie':'blah=123; etc=456;',
}

for n in range( 0, 9999 ):
    #wibble = n.zfill( 4 )
    wibble = "%04d" % n
    payload = { 
    'name':'test',
    'genNum':wibble,
    'Button1':'Push+Now'
    }
    #print payload
    #r = requests.post( url, data=payload, headers=headers, proxies=proxies, verify=False )
    future = session.post( url, data=payload, headers=headers, verify=False, background_callback=responseCallback )
    response = future.result()
    print( "%s : %s" % ( wibble, response.data ) )

理想情况下,我想修复我的实际代码,仍然使用我已经使用过的库,但如果由于某种原因它不好,我愿意接受建议......

编辑:我目前正在使用 python2 和 concurrent.futures 反向端口。

编辑:慢 - 大约每秒一个请求,并且不是并发的,而是一个接一个,所以 request1、response1、request2、response2 - 我希望它们在请求出去和进入多个线程时交错?

【问题讨论】:

  • 您能更详细地描述一下slow 的含义吗?

标签: python multithreading asynchronous python-requests


【解决方案1】:

以下代码是提交多个请求的另一种方式,一次处理多个请求,然后打印出结果。结果在准备就绪时打印,不一定与提交时的顺序相同。

它还使用大量日志记录来帮助调试问题。它捕获有效负载以进行日志记录。多线程代码很难,所以日志越多越好!

来源

import logging, sys

import concurrent.futures as cf
from requests_futures.sessions import FuturesSession

URL = 'http://localhost'
NUM = 3

logging.basicConfig(
    stream=sys.stderr, level=logging.INFO,
    format='%(relativeCreated)s %(message)s',
    )

session = FuturesSession()
futures = {}

logging.info('start')
for n in range(NUM):
    wibble = "%04d" % n
    payload = { 
        'name':'test',
        'genNum':wibble,
        'Button1':'Push+Now'
    }
    future = session.get( URL, data=payload )
    futures[future] = payload

logging.info('requests done, waiting for responses')

for future in cf.as_completed(futures, timeout=5):
    res = future.result()
    logging.info(
        "wibble=%s, %s, %s bytes",
        futures[future]['genNum'],
        res,
        len(res.text),
    )

logging.info('done!')

输出

69.3101882935 start
77.9430866241 Starting new HTTP connection (1): localhost
78.3731937408 requests done, waiting for responses
79.4050693512 Starting new HTTP connection (2): localhost
84.498167038 wibble=0000, <Response [200]>, 612 bytes
85.0481987 wibble=0001, <Response [200]>, 612 bytes
85.1981639862 wibble=0002, <Response [200]>, 612 bytes
85.2642059326 done!

【讨论】:

  • @wibble 感谢您的反馈,我已经更新了代码,因此它可以输出“wibble”参数
  • 测试它并且它有效,所以接受。如果你能看到的话,我会喜欢我的一个/版本出了什么问题的一个简单的/oneliner。如果不是 - 没问题。非常感谢!
  • 如果我将 num 更改为 999,它几乎总是会因“concurrent.futures._base.TimeoutError: 9722 (of 9999) futures unfinished”而崩溃......你会碰巧知道为什么或应该我问一个单独的问题?我什至将您的“超时=”行更改为 10 秒...
  • 关于TimeoutError:我误读了文档,as_completed所有 请求的超时,而不是一次一个。只需忽略超时,事情就会变得更好。 docs.python.org/dev/library/…
  • @johntellsall,谢谢!我遇到的一个问题是一段时间后我得到一个重置的连接日志,这会导致 python 崩溃——这有什么线索吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-15
  • 2014-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多