gpt4 book ai didi

multithreading - Parallel For Each with a Thread.Sleep

转载 作者:行者123 更新时间:2023-12-03 12:47:19 27 4
gpt4 key购买 nike

基本上,我想从同一个 URl 访问很多页面,但我不想在同一秒内发送垃圾邮件。而且我不想在 30 分钟内结束。
并行内部的 Thread.Sleep() 工作正常吗?
因为只有在大约 90 分钟后我才完成。

        var pages = new List<string>( urls );
var sources = new BlockingCollection<string>();

string htmlCode = "";

Random randomN = new Random();

Parallel.ForEach(pages, x =>
{
x = x.Replace("//", "http://");
int sleep = randomN.Next(0, 1800000);
Thread.Sleep(sleep);

HttpWebResponse response = null;
CookieContainer cookie = new CookieContainer();

HttpWebRequest newRequest = (HttpWebRequest)WebRequest.Create(x);
newRequest.Timeout = 15000;
newRequest.Proxy = null;
newRequest.CookieContainer = cookie;
newRequest.UserAgent = "Mozilla / 5.0(Windows NT 5.1) AppleWebKit / 537.11(KHTML, like Gecko) Chrome / 23.0.1300.0 Iron / 23.0.1300.0 Safari / 537.11";
newRequest.ContentLength = 0;
response = (HttpWebResponse)newRequest.GetResponse();
htmlCode = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(1252)).ReadToEnd();
sources.Add(htmlCode);

});

return sources;

最佳答案

但是,对于您要实现的目标,要求不是很清楚。但是 Thread.Sleep in parallel.foreach 可能不是正确的选择。尝试通过以下线程,它们可能会有所帮助并解决您的问题。

[1]。 Parallel.ForEach using Thread.Sleep equivalent

[2]。 Thread.Sleep blocking parallel execution of tasks

[3]。 Parallel ForEach wait 500 ms before spawning

关于multithreading - Parallel For Each with a Thread.Sleep,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33903243/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com