Parallel.ForEachAsync 需要帮助
好的。因此,我将首先向前道歉,据我所知,关于并行
和async有很多问题。但是,即使在搜索之后,我也无法围绕该如何工作。编码是我日复一日地做的事情。
我正在尝试帮助朋友使用Alphavantage API收集一些历史性的库存数据。
我在收集数据并将其保存到数据库时没有问题,但是要花20年的每日价格可能需要很长时间。因此,为了避免GUI冻结,我使其成为异步功能。但是,考虑到需要拉出多少数据,需要并行完成。我不确定有多少平行事件,但长期目标是在应用程序中将计时器置于时间间隔中,并调用batchupdateStockhistoryAsyAsyAsyAsyAsyAsync
方法,并通过一个值来通过多少股票来拉动然后在此批处理中更新该功能,然后将出现,查询数据库并获取具有最古老更新的列表5。
目前,我将所有这些简单地剥离了这个问题,只是创建了5个迭代的手动列表。对于列表中的每个股票,它调用另一个函数PullandWriteHistoricDailyData(item)
,它可以实际联系到Alphavantage并更新DB。
在这个冗长的信息之后,我的问题是,最好的方法是触发pullandwritehistoricdailydataa()
的多个并发线程?
我想我还需要使用maxdegreofparallelism
来弄清楚什么最有效,但我甚至还没有走那么远。我相信我想使用Parallel.ForeachAsync
?但是只是不确定如何将它们全部拉在一起。
任何帮助将不胜
感激
public async Task BatchUpdateStockHistoryAsync()
{
List<string> list = new List<string>();
list.Add("AAPL");
list.Add("F");
list.Add("MSFT");
list.Add("COKE");
list.Add("IAG");
foreach(string item in list)
{
await Task.Run(() => PullAndWriteHistoricDailyData(item));
}
return completed;
}
Ok. So I am going to start by apologizing up front as I know there have been a lot of question asked about Parallel
and Async. However, even after searching I can not wrap my brain around how this should work. Coding is something I dabble in not something I do day in and day out.
I am trying to help a friend collect some historic stock data using the AlphaVantage API.
I have no issues collecting the data and saving it to the database but it can take a long time to pull 20 years of daily prices. So to avoid the GUI freezing up I made it an async function. However, Considering how often and how much data needs to be pulled it will need to be done in parallel. I am not sure how many parallel events yet but the long term goal is to have a timer in the application that fires off at intervals and calls the BatchUpdateStockHistoryAsync
method and passes in a value for how many stocks to pull and update in this batch the function will then go out, query the DB and get a list 5 that have the oldest update.
For the moment I stripped all of that out to simply the question and just created a manual list of 5 stocks that get iterated through. For each stock in the list it calls another function PullAndWriteHistoricDailyData(item)
that does all the work of actually reaching out to AlphaVantage and updating the DB.
After this lengthy info my question is what's the best way is to trigger multiple concurrent threads of PullAndWriteHistoricDailyData()
?
I imagine I will need to also play with MaxDegreeOfParallelism
to figure out what works best but I have not even gotten that far yet. I believe I would want to use Parallel.ForEachAsync
maybe? but just not sure how to pull it all together.
Any help would be greatly appreciated
Misiu
public async Task BatchUpdateStockHistoryAsync()
{
List<string> list = new List<string>();
list.Add("AAPL");
list.Add("F");
list.Add("MSFT");
list.Add("COKE");
list.Add("IAG");
foreach(string item in list)
{
await Task.Run(() => PullAndWriteHistoricDailyData(item));
}
return completed;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
编辑我最初错误地阅读了
Task.WhenAll
与Parallel.ForEach
的内容。根据 Theodore Zoulias 的回答阅读有关Parallel.ForEachAsync
的更多信息后:is-parallel-foreachasync- a-replacement-to-a-plain-for-loop-append-to-task-list,并基于您想要查看每个任务的结果的假设要通过调用Parallel.ForEachAsync
接收单个任务,那么我建议更改为使用以下内容:您可以切换到使用
List
并使用 < code>await Task.WhenAll() 并跳过Parallel.ForEachAsync
,如下所示:如果
PullAndWriteHistoricDailyData()
已经是async
方法,您不需要使用Task.Run
,如果您是 ,您可以执行与上面相同的操作,减去
Task.Run
打算使用 Parallel.ForEachAsync,这篇文章很好地解释了它:parallelforeachasync-in-net-6EDIT I mistakenly read about
Task.WhenAll
vsParallel.ForEach
originally. After reading more aboutParallel.ForEachAsync
based on the answer by Theodore Zoulias at: is-parallel-foreachasync-a-replacement-to-a-plain-for-loop-append-to-task-list, and based on the assumption that you want to view the result of each Task as apposed to receiving a single Task from the call toParallel.ForEachAsync
, then I would propose changing to using the following:You could switch to using a
List<Task>
and useawait Task.WhenAll()
and skip theParallel.ForEachAsync
like so:if
PullAndWriteHistoricDailyData()
is already anasync
method, you don't need to useTask.Run
, you could do the same as above minus theTask.Run
if you are intent on using
Parallel.ForEachAsync
, this article explains it pretty well: parallelforeachasync-in-net-6