Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用Foreach.Parallel迭代两个列表_C#_.net_Foreach_Parallel Processing - Fatal编程技术网

C# 如何使用Foreach.Parallel迭代两个列表

C# 如何使用Foreach.Parallel迭代两个列表,c#,.net,foreach,parallel-processing,C#,.net,Foreach,Parallel Processing,我面临着相当大的困难,我想使用Parallel.Foreach遍历一个列表 想象一下 static List<string> proxyList = new List<string>(); static List<string> websiteList = new List<string>(); 以及网站列表 https://google.com https://spotify.com https://duckduckgo.com https:/

我面临着相当大的困难,我想使用
Parallel.Foreach
遍历一个列表

想象一下

static List<string> proxyList = new List<string>();
static List<string> websiteList = new List<string>();
以及网站列表

https://google.com
https://spotify.com
https://duckduckgo.com
https://amazon.com
我想实现这样的目标,但我不知道如何实现,无论我如何扭转,我似乎找不到任何逻辑

Foreach(var proxy in proxyList)
{
    If(proxyIsAlive)
        //Try to connect to the first website in the website list
    else
       //Try the next proxy until I get a working one 
       //and then try to connect to the most recent one
    }
 }
我面临的问题是,我必须知道如何访问网站列表中我想连接的网站

编辑:这就是我目前的逻辑

private static void Connect()
{
   string tproxy = "";
   int port;

   foreach (var website in websiteList)
   {
      foreach (var proxy in proxyList)
      {
         var proxySplit = proxy.Split(':');
         tproxy = proxySplit[0];
         port = Convert.ToInt32(proxySplit[1]);
         //if(ProxyIsAlive)

      }
      //Use that proxy down here to connect
   }
}

如果ProxyIsAlive返回true,我只想移出proxy foreach,所以这就是我最后要做的

private static void ConnectToWebsite()
        {
            var proxyIP = "";
            int port;

            foreach (var website in WebsiteList)
            {
                foreach (var proxy in proxyList)
                {
                    var proxySplit = proxy.Split(':');
                    proxyIP = proxySplit[0];
                    var convert = Int32.TryParse(proxySplit[1], out port);
                    if(HttpClientExtension.GetHttpResponse(getCMYIP, proxyIP, port))
                        Console.WriteLine(website + proxy);
                }
            }
        }
它将检查代理,直到找到一个工作代理。 现在我需要使其异步以加快速度。

注意:

  • 根据您的原始问题,这将使用嵌套并行
  • 它假设您想先检查最后一个好的代理
  • 如果找到一个好的代理,它会更新成功时间
  • 它并行处理每个网站和代理
注意:Parallel.ForEach适用于CPU受限的任务,您需要 注意不要浪费资源阻塞线程等待 要完成的IO操作

类来保存代理信息

public class Proxy
{
   public string Host { get; set; }
   public int Port { get; set; }
   public DateTime LastSuccess { get; set; }

   public Proxy(string value)
   {
      var proxySplit = value.Split(':');
      Host = proxySplit[0];
      Port = Convert.ToInt32(proxySplit[1]);
      LastSuccess = DateTime.MinValue;
   }
}
并行运行的代码

var proxies = proxyList.Select(x => new Proxy(x)).ToList();

Parallel.ForEach(webSites, new ParallelOptions { MaxDegreeOfParallelism = 4 }, site =>
   {
      Parallel.ForEach(proxies.OrderByDescending(x => x.LastSuccess), new ParallelOptions { MaxDegreeOfParallelism = 4 }, proxy =>
      {               
         if(!CheckProxy(proxy))
         {
            //check next proxy
            return;
         }
         // if we found a good proxy
         // update the lastSuccess so we check that first
         proxy.LastSuccess = DateTime.Now;
         // do something to the website 
      });
   });
}
注意:这可能不是最好的方法,如果您有CPU绑定的代码, 平行性是适当的;如果您有I/O绑定的代码,则异步是必需的 适当的在这种情况下,HttpClientExtension.GetHttpResponse是 显然是I/O,因此理想的消费代码应该是异步的

我会考虑对IO绑定操作<强/> /P>的主题<强>并行执行 现有的问题涉及到这一点,例如


什么不起作用?@Saruman不是因为它不起作用,而是因为我找不到逻辑来构造我想构造的东西你想做什么?检查一个代理是否工作,如果它工作,则执行其他一些并行操作?想象一下,我有一个代理列表,我想一次检查4个代理是否工作,因此与4选项并行,然后如果它找到一个工作的代理,我想使用该工作的代理连接到一个网站,那么假设这4个代理中有2个工作,我想连接到两个网站使用这些代理,网站都是不同的,他们在一个列表中存储。我说得通吗?列表中第一个未使用的网站?(即阻止访问该网站,直到它完成)或按顺序访问它们,直到它们都被使用过一次
var proxies = proxyList.Select(x => new Proxy(x)).ToList();

Parallel.ForEach(webSites, new ParallelOptions { MaxDegreeOfParallelism = 4 }, site =>
   {
      Parallel.ForEach(proxies.OrderByDescending(x => x.LastSuccess), new ParallelOptions { MaxDegreeOfParallelism = 4 }, proxy =>
      {               
         if(!CheckProxy(proxy))
         {
            //check next proxy
            return;
         }
         // if we found a good proxy
         // update the lastSuccess so we check that first
         proxy.LastSuccess = DateTime.Now;
         // do something to the website 
      });
   });
}