批量处理数据

        private readonly SemaphoreSlim _throttler = new SemaphoreSlim(16);//允许指定同时可以访问资源的线程数。
        public async Task BatchTaskTest<T>(List<T> lst, int batchSize = 100)
        {
            var batches = lst.Select((item, index) => new { item, index })
            .GroupBy(x => x.index / batchSize)
            .Select(x => x.Select(f => f.item).ToList())
            .ToArray();//经典

            foreach (var datas in batches)
            {
                var tasks = new Task[datas.Count];
                try
                {
                    for (int i = 0; i < datas.Count; i++)
                    {
                        tasks[i] = PrivateTask(datas[i]);
                    }
                    await Task.WhenAll(tasks);
                }
                catch (Exception e)
                {
                    await Loger.WriteLog("XXXXX", e);
                }
            }
        }

        private async Task PrivateTask<T>(T item)
        {
            await _throttler.WaitAsync();
            try
            {
                Thread.Sleep(2000);//待执行任务
            }
            finally
            {
                _throttler.Release();
            }
        }

 

posted @ 2024-09-27 09:55  C_MZ  阅读(20)  评论(0)    收藏  举报