Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.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
使用ipython加速并行数据下载_Ipython - Fatal编程技术网

使用ipython加速并行数据下载

使用ipython加速并行数据下载,ipython,Ipython,我有很多(~1000)小文件要下载。我已经为此编写了一个函数,以便能够使用map。下载函数本身使用的请求大大提高了urllib2的稳定性,urllib2给了我很多超时时间。 但是,与运行串行映射相比,在例如4个进程上并行运行时,会有较小的加速: data = map(get_data, IDs) data = dview.map_sync(get_data, IDs) 我不确定: 地图同步是最好的吗?我考虑过使用map_async,但我需要完整的列表,这样就不会有什么不同了 还可以做些什么来

我有很多(~1000)小文件要下载。我已经为此编写了一个函数,以便能够使用map。下载函数本身使用的请求大大提高了urllib2的稳定性,urllib2给了我很多超时时间。 但是,与运行串行映射相比,在例如4个进程上并行运行时,会有较小的加速:

data = map(get_data, IDs)
data = dview.map_sync(get_data, IDs)
我不确定:

  • 地图同步是最好的吗?我考虑过使用map_async,但我需要完整的列表,这样就不会有什么不同了
  • 还可以做些什么来加快这一进程
  • 我的期望是并行地同时执行n次下载,而不是一次又一次

由于您的下载受到IO限制,我建议您使用一个简单的线程池,而不是IPython.parallel(注意:我是IPython.parallel的作者)。这样做容易得多,而且IPython.parallel所做的任何事情都不会真正有利于您所介绍的案例

我设置了一个响应测试请求的程序

测试对我的慢速服务器的简单请求 它只是用请求的号码回复任何请求的
/NUMBER
, 但服务器在处理请求时人为地变慢:

import requests

r = requests.get("http://localhost:8888/10")
r.content

'10'
我们的
get_data
函数下载给定ID的URL, 并解析结果(将int的str强制转换为int):

现在使用线程池进行测试,使用不同数量的并发线程获取一组数据:

from multiprocessing.pool import ThreadPool

IDs = range(128)
for nthreads in [1, 2, 4, 8, 16, 32]:
    pool = ThreadPool(nthreads)
    tic = time.time()
    results = pool.map(get_data, IDs)
    toc = time.time()
    print "%3i threads: %5.1f seconds" % (nthreads, toc-tic)


  1 threads:  26.2 seconds
  2 threads:  13.3 seconds
  4 threads:   6.7 seconds
  8 threads:   3.4 seconds
 16 threads:   1.8 seconds
 32 threads:   1.1 seconds
您可以使用它来计算有多少线程对您的案例有意义。您还可以轻松地将ThreadPool替换为ProcessPool,并查看是否获得更好的结果

这个例子

from multiprocessing.pool import ThreadPool

IDs = range(128)
for nthreads in [1, 2, 4, 8, 16, 32]:
    pool = ThreadPool(nthreads)
    tic = time.time()
    results = pool.map(get_data, IDs)
    toc = time.time()
    print "%3i threads: %5.1f seconds" % (nthreads, toc-tic)


  1 threads:  26.2 seconds
  2 threads:  13.3 seconds
  4 threads:   6.7 seconds
  8 threads:   3.4 seconds
 16 threads:   1.8 seconds
 32 threads:   1.1 seconds