control the number of simultaneous image operations

This commit is contained in:
Luke Pulverenti
2015-04-23 12:50:54 -04:00
parent 7ac8fd1c68
commit 6f01652520
7 changed files with 84 additions and 47 deletions

View File

@@ -51,6 +51,7 @@ namespace Emby.Drawing
private readonly IJsonSerializer _jsonSerializer;
private readonly IServerApplicationPaths _appPaths;
private readonly IImageEncoder _imageEncoder;
private readonly SemaphoreSlim _imageProcessingSemaphore;
public ImageProcessor(ILogger logger, IServerApplicationPaths appPaths, IFileSystem fileSystem, IJsonSerializer jsonSerializer, IImageEncoder imageEncoder)
{
@@ -88,6 +89,8 @@ namespace Emby.Drawing
}
_cachedImagedSizes = new ConcurrentDictionary<Guid, ImageSize>(sizeDictionary);
var count = Environment.ProcessorCount;
_imageProcessingSemaphore = new SemaphoreSlim(count, count);
}
public string[] SupportedInputFormats
@@ -201,6 +204,8 @@ namespace Emby.Drawing
await semaphore.WaitAsync().ConfigureAwait(false);
var imageProcessingLockTaken = false;
try
{
CheckDisposed();
@@ -212,11 +217,20 @@ namespace Emby.Drawing
Directory.CreateDirectory(Path.GetDirectoryName(cacheFilePath));
await _imageProcessingSemaphore.WaitAsync().ConfigureAwait(false);
imageProcessingLockTaken = true;
_imageEncoder.EncodeImage(originalImagePath, cacheFilePath, newWidth, newHeight, quality, options);
}
}
finally
{
if (imageProcessingLockTaken)
{
_imageProcessingSemaphore.Release();
}
semaphore.Release();
}
@@ -254,10 +268,15 @@ namespace Emby.Drawing
return GetResult(croppedImagePath);
}
var imageProcessingLockTaken = false;
try
{
Directory.CreateDirectory(Path.GetDirectoryName(croppedImagePath));
await _imageProcessingSemaphore.WaitAsync().ConfigureAwait(false);
imageProcessingLockTaken = true;
_imageEncoder.CropWhiteSpace(originalImagePath, croppedImagePath);
}
catch (Exception ex)
@@ -269,6 +288,11 @@ namespace Emby.Drawing
}
finally
{
if (imageProcessingLockTaken)
{
_imageProcessingSemaphore.Release();
}
semaphore.Release();
}
@@ -592,13 +616,25 @@ namespace Emby.Drawing
return enhancedImagePath;
}
var imageProcessingLockTaken = false;
try
{
Directory.CreateDirectory(Path.GetDirectoryName(enhancedImagePath));
await _imageProcessingSemaphore.WaitAsync().ConfigureAwait(false);
imageProcessingLockTaken = true;
await ExecuteImageEnhancers(supportedEnhancers, originalImagePath, enhancedImagePath, item, imageType, imageIndex).ConfigureAwait(false);
}
finally
{
if (imageProcessingLockTaken)
{
_imageProcessingSemaphore.Release();
}
semaphore.Release();
}
@@ -717,9 +753,18 @@ namespace Emby.Drawing
return Path.Combine(path, filename);
}
public void CreateImageCollage(ImageCollageOptions options)
public async Task CreateImageCollage(ImageCollageOptions options)
{
_imageEncoder.CreateImageCollage(options);
await _imageProcessingSemaphore.WaitAsync().ConfigureAwait(false);
try
{
_imageEncoder.CreateImageCollage(options);
}
finally
{
_imageProcessingSemaphore.Release();
}
}
public IEnumerable<IImageEnhancer> GetSupportedEnhancers(IHasImages item, ImageType imageType)