diff --git a/Kyoo.Common/Utility.cs b/Kyoo.Common/Utility.cs index 01dc58d8..36cd6f3c 100644 --- a/Kyoo.Common/Utility.cs +++ b/Kyoo.Common/Utility.cs @@ -207,5 +207,22 @@ namespace Kyoo for (int i = 0; i < list.Count; i += countPerList) yield return list.GetRange(i, Math.Min(list.Count - i, countPerList)); } + + public static IEnumerable BatchBy(this IEnumerable list, int countPerList) + { + T[] ret = new T[countPerList]; + int i = 0; + + using IEnumerator enumerator = list.GetEnumerator(); + while (enumerator.MoveNext()) + { + ret[i] = enumerator.Current; + i++; + if (i < countPerList) + continue; + i = 0; + yield return ret; + } + } } } \ No newline at end of file diff --git a/Kyoo/Tasks/Crawler.cs b/Kyoo/Tasks/Crawler.cs index 6ffb7df0..79238752 100644 --- a/Kyoo/Tasks/Crawler.cs +++ b/Kyoo/Tasks/Crawler.cs @@ -113,20 +113,17 @@ namespace Kyoo.Controllers .ToList(); // Todo batch wth higher numbers per list once multi-services has been implemented. - - List tasks = shows + + IEnumerable tasks = shows .Select(x => x.First()) - .Select(x => RegisterFile(x, x.Substring(path.Length), library, cancellationToken)) - .ToList(); - // TODO EXECUTION OF THE TASKS ARE CALCULATED DURING THE .ToList() CALL, NO BACHING/ASYNC IS DONE. - foreach (List showTasks in tasks.BatchBy(1)) + .Select(x => RegisterFile(x, x.Substring(path.Length), library, cancellationToken)); + foreach (Task[] showTasks in tasks.BatchBy(30)) await Task.WhenAll(showTasks); - + tasks = shows .SelectMany(x => x.Skip(1)) - .Select(x => RegisterFile(x, x.Substring(path.Length), library, cancellationToken)) - .ToList(); - foreach (List episodeTasks in tasks.BatchBy(1)) + .Select(x => RegisterFile(x, x.Substring(path.Length), library, cancellationToken)); + foreach (Task[] episodeTasks in tasks.BatchBy(50)) await Task.WhenAll(episodeTasks); } }