Search code examples
c#multithreadingthread-safetyparallel.foreach

C# return string of proxy from list non-repeat / thread-safe


Basically, I found out that my web requests are only using the same proxy over and over in a web scraping project I'm doing.

public static List<string> proxyLogs = new List<string>();
private static Random random = new Random();

public static string randomizeProxy(List<string> proxies = null)
{
    if (proxies == null)
       proxies = proxyLogs;

    return proxies[random.Next(proxies.Count)];
}

Parallel.ForEach(concurrentLogs, new ParallelOptions { MaxDegreeOfParallelism = 4}, log =>
{
//my http requests
string proxyLog = randomizeProxy(proxyLogs);
Console.WriteLine(proxyLog);
});

So the parallel option thread is set to 4, the 4 requests its doing is using the same proxy over and over and not different for each thread.

What seems to be the best approach for this?


Solution

  • Anything that doesn't require parallel, put outside the ForEach. There is no reason random number selection needs to go in there (especially since it isn't thread-safe).

    var data = concurrentLogs.Select
    (
        log => new { Log = log, Proxy = randomizeProxy(proxyLogs) } 
    ).ToList();
    Parallel.ForEach( data, new ParallelOptions (MaxDegreeOfParallelism = 4}, item =>
    {
        var log = item.Log;
        var proxyLog = item.Proxy;
        Console.WriteLine(proxyLog);
    });