The crawler is now fully multi-threaded

This commit is contained in:
Zoe Roux 2020-06-18 14:54:54 +02:00
parent 8e37505b8f
commit ef84a969b7
2 changed files with 24 additions and 10 deletions

View File

@ -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<T[]> BatchBy<T>(this IEnumerable<T> list, int countPerList)
{
T[] ret = new T[countPerList];
int i = 0;
using IEnumerator<T> enumerator = list.GetEnumerator();
while (enumerator.MoveNext())
{
ret[i] = enumerator.Current;
i++;
if (i < countPerList)
continue;
i = 0;
yield return ret;
}
}
}
}

View File

@ -113,20 +113,17 @@ namespace Kyoo.Controllers
.ToList();
// Todo batch wth higher numbers per list once multi-services has been implemented.
List<Task> tasks = shows
IEnumerable<Task> 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<Task> 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<Task> 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);
}
}