简体   繁体   English

Windows Phone,多个HTTP请求并行,多少?

[英]Windows Phone, Multiple HTTP request parallel, how many?

In my Windows Phone 8 app, Im fetching list of items from web api. 在我的Windows Phone 8应用程序中,我从web api获取项目列表。 After that I loop all items and get details for each Item. 之后,我循环所有项目并获取每个项目的详细信息。

Right now my code is something like this: 现在我的代码是这样的:

List<plane> planes = await planeService.getPlanes(); // Get all planes from web api

foreach(Plane plane in planes)
{
    var details = await planeService.getDetails(plane.id); // Get one plane details from web api

    drawDetails(details);
}

How can I improve this to make multiple request in parallel and what is resonable number of request running parallel? 如何改进此功能以并行生成多个请求以及并行运行的请求数量是多少? The planes list can be anything from 0 to 100 objects, typically max 20. 飞机列表可以是0到100个对象,通常最多20个。

How can I improve this to make multiple request in parallel? 如何改进此功能以并行提出多个请求?

You can do the parallel processing like below (untested). 您可以执行如下所示的并行处理(未经测试)。 It uses SemaphoreSlim to throttle getDetails requests. 它使用SemaphoreSlim来限制getDetails请求。

async Task ProcessPlanes()
{
    const int MAX_REQUESTS = 50;

    List<plane> planes = await planeService.getPlanes(); // Get all planes from web api

    var semaphore = new SemaphoreSlim(MAX_REQUESTS);

    Func<string, Task<Details>> getDetailsAsync = async (id) =>
    {
        await semaphore.WaitAsync();
        try 
        {           
            var details = await planeService.getDetails(id);
            drawDetails(details);
            return details;
        }
        finally
        {
            semaphore.Release();
        }
    };

    var tasks = planes.Select((plane) => 
        getDetailsAsync(plane.id));

    await Task.WhenAll(tasks.ToArray());
}

what is resonable number of request running parallel? 并行运行请求的合理数量是多少? The planes list can be anything from 0 to 100 objects, typically max 20. 飞机列表可以是0到100个对象,通常最多20个。

It largely dependents on the server, but I don't think there's an ultimate answer to this. 它在很大程度上取决于服务器,但我认为没有最终的答案。 For example, check this question: 例如,检查此问题:

A reasonable number of simultaneous, asynchronous ajax requests 合理数量的同步,异步Ajax请求

As far as the WP8 client goes, I believe it can spawn 100 parallel requests without a problem. 就WP8客户端而言,我相信它可以毫无问题地产生100个并行请求。

I don't know what the limit is for network connections, but there will be one. 我不知道网络连接的限制是什么,但会有一个。

If there wasn't, the only problem would be the amount of memory used to keep that many requests alive. 如果没有,唯一的问题是用于保持许多请求存活的内存量。

So, assuming the underlying operating system will handle throttling properly, I would do something this: 因此,假设底层操作系统将正确处理限制,我会做一些事情:

List<plane> planes = await planeService.getPlanes();

var allDetails = Task.WhenAll(from plane in plains
                              select planeService.getDetails(plane.id));

foreach(var details in allDetails)
{
    drawDetails(details);
}

NOTE: You should follow common naming conventions to help others understand your code. 注意:您应遵循常见的命名约定,以帮助其他人了解您的代码。 Asynchronous methods should be suffixed Async and, in *C# , method names are always CamelCase. 异步方法应该是Async的后缀,而在* C#中 ,方法名称总是CamelCase。

You should check the ServicePoint , this will provides connection management for HTTP connections. 您应该检查ServicePoint ,这将为HTTP连接提供连接管理。 The default maximum number of concurrent connections allowed by a ServicePoint object is 2. So if you need to increase it you can use ServicePointManager.DefaultConnectionLimit property . ServicePoint对象允许的默认最大并发连接数为2.因此,如果需要增加它,可以使用ServicePointManager.DefaultConnectionLimit属性 Just check the link in MSDN there you can see a sample. 只需检查MSDN中的链接,您就可以看到示例。 And set the value you need. 并设置您需要的值。 This might help you.. 这可能对你有帮助..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM