我正在开发一款可在Google搜索结果网址中搜索电子邮件地址的应用.问题是它需要将每个页面中找到的值+它找到电子邮件的URL返回到包含2列的数据网格视图:电子邮件和URL.我正在使用Parallel.ForEach这个,但它当然返回随机URL,而不是它真正找到的电子邮件.
public static string htmlcon; //htmlsource
public static List<string> emailList = new List<string>();
public static string Get(string url, bool proxy)
{
htmlcon = "";
try
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
if (proxy)
req.Proxy = new WebProxy(proxyIP + ":" + proxyPort);
req.Method = "GET";
req.UserAgent = Settings1.Default.UserAgent;
if (Settings1.Default.EnableCookies == true)
{
CookieContainer cont = new CookieContainer();
req.CookieContainer = cont;
}
WebResponse resp = req.GetResponse();
StreamReader SR = new StreamReader(resp.GetResponseStream());
htmlcon = SR.ReadToEnd();
Thread.Sleep(400);
resp.Close();
SR.Close();
}
catch (Exception)
{
Thread.Sleep(500); …Run Code Online (Sandbox Code Playgroud) 我正在看Igor Ostrovsky的PLINQ PCD09演示文稿,想要试着看看我能从CULV笔记本电脑中得到什么.
在某一点上,我得到了一个奇怪的例外,我不确定它是什么意思.我已经浓缩了代码以获得更好的概述.它是导致异常的最后一个primes.Sum(),如果我使范围变小 - 8000 - 则不会抛出异常.有任何想法吗?
Func<int, bool> isprime = n => // ignore input checks for now
{
int sqr = Convert.ToInt32(Math.Ceiling(Math.Sqrt(n)));
for (int i = 2; i < sqr; i++) if (n % i == 0) return false;
return true;
};
var numbers = Enumerable.Range(1, 8*1000*1000);
long counter = 0;
ParallelQuery<int> primes = numbers.AsParallel().Where(x => isprime(x));
counter = primes.Sum();
Run Code Online (Sandbox Code Playgroud)
例外(很长)
System.AggregateException未处理Message =发生一个或多个错误.Source = System.Core
StackTrace:System.Linq.Parallel.QueryTaskGroupState.QueryEnd(Boolean userInitiatedDispose)at System.Linq.Parallel.SpoolingTask.SpoolStopAndGo [TInputOutput,TIgnoreKey](QueryTaskGroupState groupState,PartitionedStream2 partitions, SynchronousChannel1 [] channels,TaskScheduler taskScheduler)at …
我正在学习PLINQ.我尝试了在网站上给出的例子.但是我可以看到结果是错误的,并且当我多次重新运行程序时也会有所不同.首先它不给出所有素数,其次它只给出9591个随机素数.
IEnumerable<int> numbers = Enumerable.Range (3, 100000-3);
var parallelQuery =
from n in numbers.AsParallel()
where Enumerable.Range (2, (int) Math.Sqrt (n)).All (i => n % i > 0)
select n;
int[] primes = parallelQuery.ToArray();
Run Code Online (Sandbox Code Playgroud)
感谢任何帮助!
这是我的代码:
Parallel.ForEach(Students, item =>
{
StudentModel studentModel = new StudentModel(item);
// Maybe he/she has alot of name
foreach (var words in studentModel.StudentNames.Split(','))
{
if (string.IsNullOrWhiteSpace(words))
return;
string tempWords = words.Trim();
// add it to student names list
STUDENT_NAMES.Where(x => x.SearchWords == tempWords).FirstOrDefault().student.Add(studentModel);
}
// add it to student list
STUDENT_MODELS.Add(studentModel);
});
Run Code Online (Sandbox Code Playgroud)
我想做的是,我得到了学生名单.将其转换为学生模型,获取学生姓名(因为一个学生有很多名字),然后我将名字添加到名单,这是因为可能以后我需要得到同名学生并做一些事情......最后添加学生到学生模特名单.
问题发生在:
STUDENT_NAMES.Where(x => x.SearchWords == tempWords).FirstOrDefault().student.Add(studentModel);
Run Code Online (Sandbox Code Playgroud)
这个地方总是发生:System.IndexOutOfRangeException
我已经将Paralle.Foreach更改为Parallel.For,并将foreach更改为for,但没有任何更改.我必须使用Parallel,因为学生数量大约为100000,如果我只使用foreach替换Parallel.Foreach,则需要160+秒,如果我锁定那个地方.....还是慢......如果使用Parallel. Foreach,它将使用20秒左右,但我无法处理异常.
我已经尝试用这个替换它:
StudentNames name = STUDENT_NAMES.Where(x => x.SearchWords == tempWords).FirstOrDefault();
if (null != name)
name.student.Add(StudentModel);
Run Code Online (Sandbox Code Playgroud)
但问题仍然发生在某些时候........如果我只是尝试...抓住并忽略它,然后当我以后访问STUDENT_NAMES列表时,它仍然抛出异常....... .....
我也尝试使用ConcurrentBag …
如果你有这个:
var resultlist = new List<Dictionary<DateTime, double>>();
Parallel.ForEach(input, item =>
{
resultlist.Add(SomeDataDictionary(item));
});
Run Code Online (Sandbox Code Playgroud)
返回数据将按方法SomeDataDictionary返回数据的顺序排列,不会按输入顺序排列.
有没有办法保持输入的顺序?
或者是更改数据类型并使用Parallel.For循环然后将索引传递给某种类型的数组返回类型的唯一方法?
我有一个需要并行运行的循环,因为每次迭代都很慢并且需要大量处理器,但我还需要调用异步方法作为循环中每次迭代的一部分。
我见过关于如何在循环中处理异步方法的问题,但没有看到关于异步和同步的组合的问题,这就是我所得到的。
我的(简化的)代码如下 - 我知道由于异步操作被传递给 foreach,这将无法正常工作。
protected IDictionary<int, ReportData> GetReportData()
{
var results = new ConcurrentDictionary<int, ReportData>();
Parallel.ForEach(requestData, async data =>
{
// process data synchronously
var processedData = ProcessData(data);
// get some data async
var reportRequest = await BuildRequestAsync(processedData);
// synchronous building
var report = reportRequest.BuildReport();
results.TryAdd(data.ReportId, report);
});
// This needs to be populated before returning
return results;
}
Run Code Online (Sandbox Code Playgroud)
当操作必须异步才能等待单个异步调用时,有什么方法可以并行执行操作。
将同步函数转换为异步函数并不是一个实用的选择。
我不想将操作拆分并使用 Parallel.ForEach ,然后使用 WhenAll 和另一个 Parallel.ForEach 进行异步调用,因为每个阶段的速度在不同迭代之间可能差异很大,因此拆分操作效率较低,因为速度越快那些人会等待较慢的人然后再继续。
我确实想知道是否可以使用 PLINQ ForAll 来代替 Parallel.ForEach,但从未使用过 PLINQ,并且不确定它是否会在返回之前等待所有迭代完成,即任务是否仍在运行结束的过程。