NSOperationQueue 中多个 NSOperation 实例中的 NSUrlConnection

发布于 2024-11-01 10:02:44 字数 2422 浏览 2 评论 0原文

以下代码在 NSOperationQueue 中添加多个 NSOperation 实例。该操作仅获取 url 的内容。我也提供了 php 代码...

给定以下代码...

-(void)requestResponse {

    NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.someurl.gr/test.php"] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:40.0];

    NSDate *dd = [NSDate date];

    NSURLResponse *resp;
    NSData *returnedData = [NSURLConnection sendSynchronousRequest:req returningResponse:&resp error:NULL];

    NSString *ss = [[[NSString alloc] initWithData:returnedData encoding:NSUTF8StringEncoding] autorelease];

    NSLog(@"%@ - %.2f",ss , -[dd timeIntervalSinceNow]);

}


-(NSOperation*)task {
    NSInvocationOperation* theOp = [[[NSInvocationOperation alloc] initWithTarget:self selector:@selector(requestResponse) object:nil] autorelease];
    return theOp;
}


-(IBAction)buttonAction:(id)sender {

    NSOperationQueue *opq = [[NSOperationQueue alloc] init];
    [opq setMaxConcurrentOperationCount:40];

    for(int i=0; i<15;i++) {
        [opq addOperation:[self task]];
        [NSThread sleepForTimeInterval:1.0]; // here is the issue!
    }

    [opq release];
}

test.php 女巫 -requestResponse 调用的内容:

<?php
    echo "Through!";
    for($i=0;$i<1000000;$i++) { // don't return too soon
    }
?>

问题是当我使用 [NSThread sleepForTimeInterval:1.0]为了在队列中添加每个 NSOperation 之前创建延迟,所有请求大约需要相同的时间才能完成。如果我评论这一行,大多数请求都需要越来越越来越多的时间才能完成。问题是为什么?

我已经从命令行(使用curl)测试了url,并且对于任意数量的同时调用php,请求需要相同的时间才能完成,因此问题不是服务器端的。

这是使用 localhost 作为服务器的输出 禁用 sleepForTimeInterval

[s] Through! - 0.22
[s] Through! - 0.23
[s] Through! - 0.25
[s] Through! - 0.26
[s] Through! - 0.26
[s] Through! - 0.28
[s] Through! - 0.43
[s] Through! - 0.46
[s] Through! - 0.49
[s] Through! - 0.50
[s] Through! - 0.52
[s] Through! - 0.51
[s] Through! - 0.60
[s] Through! - 0.62
[s] Through! - 0.63

// 当然,使用 php 做一些实际工作时,差异要大得多(从 7 到 20 秒!)。

并启用sleepForTimeInterval

[s] Through! - 0.23
[s] Through! - 0.09
[s] Through! - 0.09
[s] Through! - 0.09
[s] Through! - 0.09
[s] Through! - 0.09
[s] Through! - 0.08
[s] Through! - 0.09
[s] Through! - 0.09
[s] Through! - 0.08
[s] Through! - 0.09
[s] Through! - 0.09
[s] Through! - 0.09
[s] Through! - 0.09
[s] Through! - 0.09

The following code adds multiple NSOperation instances in an NSOperationQueue. The Operation just takes the contents of a url. I'm providing the php code too...

Given the following code...

-(void)requestResponse {

    NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.someurl.gr/test.php"] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:40.0];

    NSDate *dd = [NSDate date];

    NSURLResponse *resp;
    NSData *returnedData = [NSURLConnection sendSynchronousRequest:req returningResponse:&resp error:NULL];

    NSString *ss = [[[NSString alloc] initWithData:returnedData encoding:NSUTF8StringEncoding] autorelease];

    NSLog(@"%@ - %.2f",ss , -[dd timeIntervalSinceNow]);

}


-(NSOperation*)task {
    NSInvocationOperation* theOp = [[[NSInvocationOperation alloc] initWithTarget:self selector:@selector(requestResponse) object:nil] autorelease];
    return theOp;
}


-(IBAction)buttonAction:(id)sender {

    NSOperationQueue *opq = [[NSOperationQueue alloc] init];
    [opq setMaxConcurrentOperationCount:40];

    for(int i=0; i<15;i++) {
        [opq addOperation:[self task]];
        [NSThread sleepForTimeInterval:1.0]; // here is the issue!
    }

    [opq release];
}

The contents of the test.php witch -requestResponse calls:

<?php
    echo "Through!";
    for($i=0;$i<1000000;$i++) { // don't return too soon
    }
?>

The issue is that when i use [NSThread sleepForTimeInterval:1.0] to create a delay before each NSOperation addition in the queue, all requests take about the same time to complete. If I comment this line most requests take considerably increasingly more and more time to complete. The question is why?

I have tested the url from command line (using curl) and the requests take the same time to complete for any number of simultaneous calls to the php, so the issue is not server side.

Here is the output using localhost as server with sleepForTimeInterval disabled

[s] Through! - 0.22
[s] Through! - 0.23
[s] Through! - 0.25
[s] Through! - 0.26
[s] Through! - 0.26
[s] Through! - 0.28
[s] Through! - 0.43
[s] Through! - 0.46
[s] Through! - 0.49
[s] Through! - 0.50
[s] Through! - 0.52
[s] Through! - 0.51
[s] Through! - 0.60
[s] Through! - 0.62
[s] Through! - 0.63

// of course the differences is much more higher (from 7 to 20 secs!) with a php doing some real work.

and with sleepForTimeInterval enabled

[s] Through! - 0.23
[s] Through! - 0.09
[s] Through! - 0.09
[s] Through! - 0.09
[s] Through! - 0.09
[s] Through! - 0.09
[s] Through! - 0.08
[s] Through! - 0.09
[s] Through! - 0.09
[s] Through! - 0.08
[s] Through! - 0.09
[s] Through! - 0.09
[s] Through! - 0.09
[s] Through! - 0.09
[s] Through! - 0.09

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故事↓在人 2024-11-08 10:02:44

当您启用 sleepForTimeInterval 时,它会暂停比 NSOperation 完成所需的时间更长的时间。因此,一次实际上只发生一个并发操作。

当您禁用 sleepForTimeInterval 时,您将向 NSOperationQueue 提供多个要完成的并行操作。

您将 [NSoperationQueue setMaxConcurrentOperationCount] 设置为什么?我确保将其设置为 15,以允许所有操作并行完成。我想也许这些操作目前正在排队,因此并不是所有操作都并行运行。

When you have sleepForTimeInterval enabled it pauses for longer then the NSOperation takes to complete. Therefore, you really only have one concurrent operation happening at a time.

When you have sleepForTimeInterval disabled, you are presenting NSOperationQueue with multiple parallel operations to complete.

What do you have [NSoperationQueue setMaxConcurrentOperationCount] set to? I'd ensure its set to 15 to allow all operations complete in parallel. I'm thinking that perhaps the operations are queuing at the moment and thus not all running in parallel.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文