How to keep the list in multi-point mode



  • Got a list. lst♪ He's got a list of urns. Through Parallel.For I'm making queries and manipulating data. As we understand the data from the list, they are taken by accident and it is not possible to proceed normally. Is there any way that Parallel.For Figured in turn, or some other way to maintain the cycle position for all flows.



  • I think it makes sense to move on to task-based-parallelism, since you're still loaded on the network, and don't get low-tracked.

    If ProcessUrl - Asynchronous processing function one addresses, receive the following trivial code:

    var resultTasks = lst.Select(ProcessUrl);
    var allResults = await Task.WhenAll(resultTasks);
    var successfulResults =
            allResults.Where(r => r != null) // отфильтруем неуспешные
                      .Select(r => r.Value)  // вернёмся к типу byte
                      .ToList();             // и материализуем, наконец.
    

    According to the https://msdn.microsoft.com/en-us/library/hh194766(v=vs.110).aspx ♪ Task.WhenAll returns the results with the maintenance of order:

    Result property of the returned task will be set to an array containing all of the results of the supplied tasks in the same order as they were provided


    This is an example of how one address can be treated:

    // обработка одного URL, может вернуть null при ошибке
    async Task<int?> ProcessUrl(Uri uri)
    {
        try
        {
            using (var client = new HttpClient())
            using (var stream = await client.GetStreamAsync(uri))
            {
                // окей, теперь у нас есть поток
                // интенсивный подсчёт, ограниченный сетью, делаем прямо
                byte sumOfBytes = await SumBytesInStream(stream);
                // вычисление, интенсивное по процессору, выполняем на пуле потоков
                return await Task.Run(() => EncodeByte(sumOfBytes));
            }
        }
        catch (HttpRequestException e)
        {
            return null; // неудача, вернём null
        }
    }
    

    Support functions:

    async Task<byte> SumBytesInStream(Stream s)
    {
        byte[] buf = new byte[65536];
        byte sum = 0;
        while (true)
        {
            var actuallyRead = await s.ReadAsync(buf, 0, buf.Length);
            if (actuallyRead == 0)
                return sum;
            sum += buf.Take(actuallyRead).Sum();
        }
    }
    

    byte EncodeByte(byte b)
    {
    // тут много сложной криптографии
    return b + 1;
    }




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2