public static Dictionary readAllFilesAtRootFolder(string rootFolder, string[] extensions, bool subFolders,
ConcurrentQueue fileQueue, CancellationToken cancellationToken)
{
var allFiles = new Dictionary();
IEnumerable files;
try
{
if (subFolders)
{
files = extensions.SelectMany(ext => System.IO.Directory.GetFiles(rootFolder, ext, SearchOption.AllDirectories));
}
else
{
files = extensions.SelectMany(ext => System.IO.Directory.GetFiles(rootFolder, ext, SearchOption.TopDirectoryOnly));
}
int i = 1;
foreach (var file in files)
{
if (cancellationToken.IsCancellationRequested)
break;
mediaFile mediaFile = new mediaFile();
string extension = Path.GetExtension(file).Replace(".", "").ToUpper();
// If the extension is not empty, group the files by extension
if (!string.IsNullOrEmpty(extension))
{
if (!allFiles.ContainsKey(extension))
{
allFiles[extension] = new List();
}
mediaFile.fileName = file;
//mediaFile.exif_date = getImageExifDate(file.ToString());
fileQueue.Enqueue(mediaFile);
allFiles[extension].Add(mediaFile);
}
}
return allFiles;
}
catch { }
return allFiles;
}
public static void readAllEXIFdates(ConcurrentQueue fileQueue, ConcurrentDictionary resultDictionary, CancellationToken cancellationToken, bool producerCompleted)
{
var allEXIFFiles = new Dictionary();
{
//Provide some free time for the main thread
//Thread.Sleep(100);
while (!cancellationToken.IsCancellationRequested && (!producerCompleted || !fileQueue.IsEmpty))
{
if (fileQueue.TryDequeue(out var mediaFile))
{
// Process EXIF date
mediaFile.exif_date = getImageExifDate(mediaFile.fileName);
var extension = Path.GetExtension(mediaFile.fileName).ToUpperInvariant().TrimStart('.');
if (!string.IsNullOrEmpty(extension))
{
resultDictionary.AddOrUpdate(extension,
new List { mediaFile },
(key, existingList) =>
{
existingList.Add(mediaFile);
return existingList;
});
}
}
else
{
// Thread.Sleep(100);
// Wait briefly if no files are available in the queue
}
}
}
}
readAllEXIFdates() должен завершиться, когда для параметра ProducerCompleted установлено значение true, но цикл readAllEXIFdates() никогда не видит обновления, даже ProducerCompleted обновляется как открытый параметр класса. Почему это?
[code]public static Dictionary readAllFilesAtRootFolder(string rootFolder, string[] extensions, bool subFolders, ConcurrentQueue fileQueue, CancellationToken cancellationToken) { var allFiles = new Dictionary(); IEnumerable files; try { if (subFolders) { files = extensions.SelectMany(ext => System.IO.Directory.GetFiles(rootFolder, ext, SearchOption.AllDirectories)); } else { files = extensions.SelectMany(ext => System.IO.Directory.GetFiles(rootFolder, ext, SearchOption.TopDirectoryOnly)); } int i = 1; foreach (var file in files) { if (cancellationToken.IsCancellationRequested) break;
mediaFile mediaFile = new mediaFile(); string extension = Path.GetExtension(file).Replace(".", "").ToUpper(); // If the extension is not empty, group the files by extension if (!string.IsNullOrEmpty(extension)) { if (!allFiles.ContainsKey(extension)) { allFiles[extension] = new List(); } mediaFile.fileName = file; //mediaFile.exif_date = getImageExifDate(file.ToString()); fileQueue.Enqueue(mediaFile); allFiles[extension].Add(mediaFile); } } return allFiles; } catch { } return allFiles; }
public static void readAllEXIFdates(ConcurrentQueue fileQueue, ConcurrentDictionary resultDictionary, CancellationToken cancellationToken, bool producerCompleted) { var allEXIFFiles = new Dictionary();
{ //Provide some free time for the main thread //Thread.Sleep(100); while (!cancellationToken.IsCancellationRequested && (!producerCompleted || !fileQueue.IsEmpty)) { if (fileQueue.TryDequeue(out var mediaFile)) { // Process EXIF date mediaFile.exif_date = getImageExifDate(mediaFile.fileName); var extension = Path.GetExtension(mediaFile.fileName).ToUpperInvariant().TrimStart('.');
if (!string.IsNullOrEmpty(extension)) { resultDictionary.AddOrUpdate(extension, new List { mediaFile }, (key, existingList) => { existingList.Add(mediaFile); return existingList; }); } } else { // Thread.Sleep(100); // Wait briefly if no files are available in the queue } } } } [/code] Нажатие кнопки [code]private async void button4_Click_1(object sender, EventArgs e) { string[] extenions = getSelectExtensions(chkExtensions);
label5.Text = "Raading files, please wait....";
_cancellationTokenSource = new CancellationTokenSource();
var consumerTask = Task.Run(() => readAllEXIFdates(fileQueue, resultDictionary, _cancellationTokenSource.Token, producerCompleted)); // Wait for both tasks to complete await Task.WhenAll(producerTask, consumerTask);
} [/code] readAllEXIFdates() должен завершиться, когда для параметра ProducerCompleted установлено значение true, но цикл readAllEXIFdates() никогда не видит обновления, даже ProducerCompleted обновляется как открытый параметр класса. Почему это? [code]while (!cancellationToken.IsCancellationRequested && (!producerCompleted || !fileQueue.IsEmpty)) [/code] Это цикл while, который не завершается, поскольку ProducerCompleted никогда не обновляется.
Я использую сельдерей с Redis и Rabbitmq в качестве брокеров и цветов для мониторинга. У меня есть конечная точка API для загрузки документов, которая запускает первую задачу сельдерея для обработки первоначальной партии.
Внутри этой первой задачи...
Я использую ползунок Matplotlib, чтобы сделать простой динамический график синусоидальной кривой. Я хочу изменить частоту, используя слайдер. Код выглядит так:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import...