Exemplo n.º 1
0
        public async Task RunWithChannelsAsync()
        {
            // Channels that will be used
            var postPathsChannel = Channel.CreateUnbounded <string>(new UnboundedChannelOptions()
            {
                SingleReader = false, SingleWriter = true
            });
            var frontMatterChannel = Channel.CreateBounded <(string?, FrontMatter?)>(new BoundedChannelOptions(100)
            {
                SingleReader = false, SingleWriter = false
            });
            var imagesChannel = Channel.CreateBounded <(string?, FrontMatter?, Image)>(new BoundedChannelOptions(20)
            {
                SingleReader = false, SingleWriter = false
            });

            var stopwatch = new Stopwatch();

            stopwatch.Start();

            var generator = new ThumbnailGenerator();

            var tasks = new List <Task>();

            tasks.Add(Run(1, async() =>
            {
                var postPaths = Directory.GetFiles(Constants.PostsDirectory);
                foreach (var postPath in postPaths)
                {
                    await postPathsChannel.Writer.WriteAsync(postPath);
                }
            }, postPathsChannel));

            tasks.Add(Run(2, async() =>
            {
                while (await postPathsChannel.Reader.WaitToReadAsync())
                {
                    while (postPathsChannel.Reader.TryRead(out var postPath))
                    {
                        var frontMatter = await generator.ReadFrontMatterAsync(postPath);
                        await frontMatterChannel.Writer.WriteAsync((postPath, frontMatter));
                    }
                }
            }, frontMatterChannel));
Exemplo n.º 2
0
        public async Task RunWithChannelsAsync()
        {
            var stopwatch = new Stopwatch();

            stopwatch.Start();

            var generator = new ThumbnailGenerator();

            await Channel
            .CreateBounded <string>(50000)
            .Source(Directory.GetFiles(Constants.PostsDirectory))
            .PipeAsync(
                maxConcurrency: 2,
                capacity: 100,
                transform: async postPath =>
            {
                var frontMatter = await generator.ReadFrontMatterAsync(postPath);
                return(postPath, frontMatter);
            })
            .Filter(tuple => tuple.Item2 != null)
            .PipeAsync(
                maxConcurrency: 10,
                capacity: 20,
                transform: async tuple =>
            {
                var(postPath, frontMatter) = tuple;
                var cardImage = await generator.CreateImageAsync(frontMatter !);

                return(postPath, frontMatter, cardImage);
            })
            .ReadAllAsync(async tuple =>
            {
                var(postPath, _, cardImage) = tuple;
                await generator.SaveImageAsync(cardImage, Path.GetFileName(postPath) + ".png");
            });

            stopwatch.Stop();
            _outputHelper.WriteLine("Completed in {0}", stopwatch.Elapsed);
        }