matlab同时运行多个webread

时间:2017-01-26 03:51:52

标签: matlab

我需要从五个不同的网页收集五种不同的快速实时数据源。我需要以尽可能高的频率收集数据。 如果我在连续循环中运行五个webreads,那么采样率会变慢,因为一个循环需要五个webread执行时间。

是否可以同时执行所有五个webread,这样一个循环只需要五个webread执行时间中最长的一个?

代码:

for i = 1:10000000
    webread1
    webread2
    webread3
    webread4
    webread5
    append latest webread1,2,3,4,5 data to matrix
end
谢谢你!

编辑: 我试过这样的事情,但它不会让时间缩短:

parfor i=1:1
    webread1
    webread2
    webread3
    webread4
    webread5
end

1 个答案:

答案 0 :(得分:0)

同步完成对webread的调用,这意味着webread必须等待服务器的响应在执行下一个命令之前完成。如果您希望多次同时读取多个网址,则需要使用MATLAB的parfor来并行化请求。

以下代码将在其自己的进程中执行5个不同的webread查询,以便它们可以同时执行。

urls = {'url1', 'url2', 'url3', 'url4', 'url5'};

nQueries = 10000;

% Pre-allocate the output
data = cell(numel(urls), nQueries);

parfor k = 1:numel(urls)
    for m = 1:nQueries
        % Perform the query and store the result in the data cell array
        data{k,m} = webread(urls{k});
    end
end
相关问题