问题描述
我正在从事电子商务API集成,因此我需要每天发布电子商务门户中列出的每个SKU的销售价格。 SKU数以千计(截至目前为2千,并且可能会增加)。 SKU的售价每天都在变化,我可以通过调用价格更新API来一次更新一个SKU的售价(价格更新API由电子商务门户提供,我不能更改)。
价格更新API调用大约需要2.0秒,而更新2000 SKU价格将消耗4000秒,这是不可接受的。
我的每秒事务(TPS)限制为100,可以进行100次异步调用以更新SKU价格。 这是Web API Controller方法的代码片段:
//Model Class
public class SKUPrices
{
public string skuId { get; set; }
public decimal sellingPrice { get; set; }
}
//Controller method
public async Task<HttpResponseMessage> UpdateallSkuPrices(List<SKUPrices> skuPrices)
{
List<string> errorList = new List<string>();
foreach(var price in skuPrices) {
string error = await UpdatePriceAysncRestApiCall(price);//This will invoke the RESTful API and update the selling price of the given Sku
if (error != null)
errorList.Add(error);
}
if (errorList.Count() > 0)
return Request.CreateResponse(HttpStatusCode.BadRequest,errorList);
else
return Request.CreateResponse(HttpStatusCode.OK);
}
我对线程部分的知识有限。我们可以一次从skuPrices列表中获取100条记录,并异步执行100个UpdatePriceAysncRestApiCall
API调用,并继续直到所有价格更新。如果有任何错误,则必须填充errorList并将其抛出响应。
由于我不熟悉线程技术,因此我担心使用Parallel.ForEach
方法。请帮帮我。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)