[英]How to prevent an application from terminating before the completion of all fire-and-forget tasks?
我有一个定期启动即发即忘任务的应用程序,主要用于日志记录,我的问题是当应用程序关闭时,任何当前正在运行的即发即忘任务都会中止。 我想防止这种情况发生,所以我正在寻找一种机制,让我在关闭我的应用程序之前await
所有正在运行的即发即弃操作完成。 我不想处理他们可能的异常,我不在乎这些。 我只是想让他们有机会完成(可能会超时,但这不是问题的一部分)。
你可能会争辩说,这个要求使我的任务并不是真正的即发即忘,这有一定的道理,所以我想澄清这一点:
这是该问题的最小演示:
static class Program
{
static async Task Main(string[] args)
{
_ = Log("Starting"); // fire and forget
await Task.Delay(1000); // Simulate the main asynchronous workload
CleanUp();
_ = Log("Finished"); // fire and forget
// Here any pending fire and forget operations should be awaited somehow
}
private static void CleanUp()
{
_ = Log("CleanUp started"); // fire and forget
Thread.Sleep(200); // Simulate some synchronous operation
_ = Log("CleanUp completed"); // fire and forget
}
private static async Task Log(string message)
{
await Task.Delay(100); // Simulate an async I/O operation required for logging
Console.WriteLine($"{DateTime.Now:HH:mm:ss.fff} {message}");
}
}
输出:
11:14:11.441 Starting
11:14:12.484 CleanUp started
Press any key to continue . . .
"CleanUp completed"
和"Finished"
条目不会被记录,因为应用程序过早终止,并且挂起的任务被中止。 有什么办法可以在关闭之前等待他们完成吗?
顺便说一句,这个问题的灵感来自@SHAFEESPS 最近提出的一个问题,遗憾的是该问题因不清楚而关闭。
说明:上面提供的最小示例包含单一类型的即发即忘操作,即Task Log
方法。 现实世界应用程序启动的即发即弃操作是多种且异构的。 有些甚至返回通用任务,如Task<string>
或Task<int>
。
一个即发即弃的任务也有可能会触发次要的即发即忘任务,这些任务也应该被允许开始并等待。
也许像一个等待退出的柜台之类的东西? 这仍然会很容易忘记。
我只将LogAsync
移动到它自己的方法,因为每次调用 Log 时都不需要丢弃。 我想它还可以处理如果在程序退出时调用 Log 时会发生的微小竞争条件。
public class Program
{
static async Task Main(string[] args)
{
Log("Starting"); // fire and forget
await Task.Delay(1000); // Simulate the main asynchronous workload
CleanUp();
Log("Finished"); // fire and forget
// Here any pending fire and forget operations should be awaited somehow
var spin = new SpinWait();
while (_backgroundTasks > 0)
{
spin.SpinOnce();
}
}
private static void CleanUp()
{
Log("CleanUp started"); // fire and forget
Thread.Sleep(200); // Simulate some synchronous operation
Log("CleanUp completed"); // fire and forget
}
private static int _backgroundTasks;
private static void Log(string message)
{
Interlocked.Increment(ref _backgroundTasks);
_ = LogAsync(message);
}
private static async Task LogAsync(string message)
{
await Task.Delay(100); // Simulate an async I/O operation required for logging
Console.WriteLine($"{DateTime.Now:HH:mm:ss.fff} {message}");
Interlocked.Decrement(ref _backgroundTasks);
}
}
一件合理的事情是在您的记录器中拥有内存队列(这适用于符合您的条件的其他类似功能),它是单独处理的。 然后你的日志方法就变成了这样:
private static readonly BlockingCollection<string> _queue = new BlockingCollection<string>(new ConcurrentQueue<string>());
public static void Log(string message) {
_queue.Add(message);
}
它对调用者来说非常快且无阻塞,并且在某种意义上是异步的,它在未来的某个时间完成(或失败)。 调用者不知道也不关心结果,所以这是一个即发即忘的任务。
然而,这个队列是单独地、全局地、可能在一个单独的线程中、或者通过等待(和线程池线程)单独处理(通过将日志消息插入最终目的地,如文件或数据库),这无关紧要。
然后在应用程序退出之前,您只需要通知队列处理器不需要更多项目,并等待它完成。 例如:
_queue.CompleteAdding(); // no more items
_processorThread.Join(); // if you used separate thread, otherwise some other synchronization construct.
编辑:如果您希望队列处理是异步的 - 您可以使用此AsyncCollection (可用作 nuget 包)。 然后你的代码变成:
class Program {
private static Logger _logger;
static async Task Main(string[] args) {
_logger = new Logger();
_logger.Log("Starting"); // fire and forget
await Task.Delay(1000); // Simulate the main asynchronous workload
CleanUp();
_logger.Log("Finished"); // fire and forget
await _logger.Stop();
// Here any pending fire and forget operations should be awaited somehow
}
private static void CleanUp() {
_logger.Log("CleanUp started"); // fire and forget
Thread.Sleep(200); // Simulate some synchronous operation
_logger.Log("CleanUp completed"); // fire and forget
}
}
class Logger {
private readonly AsyncCollection<string> _queue = new AsyncCollection<string>(new ConcurrentQueue<string>());
private readonly Task _processorTask;
public Logger() {
_processorTask = Process();
}
public void Log(string message) {
// synchronous adding, you can also make it async via
// _queue.AddAsync(message); but I see no reason to
_queue.Add(message);
}
public async Task Stop() {
_queue.CompleteAdding();
await _processorTask;
}
private async Task Process() {
while (true) {
string message;
try {
message = await _queue.TakeAsync();
}
catch (InvalidOperationException) {
// throws this exception when collection is empty and CompleteAdding was called
return;
}
await Task.Delay(100);
Console.WriteLine($"{DateTime.Now:HH:mm:ss.fff} {message}");
}
}
}
或者您可以使用单独的专用线程来同步处理项目,就像通常那样。
编辑 2:这是引用计数的变体,它不对“即发即弃”任务的性质做出任何假设:
static class FireAndForgetTasks {
// start with 1, in non-signaled state
private static readonly CountdownEvent _signal = new CountdownEvent(1);
public static void AsFireAndForget(this Task task) {
// add 1 for each task
_signal.AddCount();
task.ContinueWith(x => {
if (x.Exception != null) {
// do something, task has failed, maybe log
}
// decrement 1 for each task, it cannot reach 0 and become signaled, because initial count was 1
_signal.Signal();
});
}
public static void Wait(TimeSpan? timeout = null) {
// signal once. Now event can reach zero and become signaled, when all pending tasks will finish
_signal.Signal();
// wait on signal
if (timeout != null)
_signal.Wait(timeout.Value);
else
_signal.Wait();
// dispose the signal
_signal.Dispose();
}
}
您的样本变为:
static class Program {
static async Task Main(string[] args) {
Log("Starting").AsFireAndForget(); // fire and forget
await Task.Delay(1000); // Simulate the main asynchronous workload
CleanUp();
Log("Finished").AsFireAndForget(); // fire and forget
FireAndForgetTasks.Wait();
// Here any pending fire and forget operations should be awaited somehow
}
private static void CleanUp() {
Log("CleanUp started").AsFireAndForget(); // fire and forget
Thread.Sleep(200); // Simulate some synchronous operation
Log("CleanUp completed").AsFireAndForget(); // fire and forget
}
private static async Task Log(string message) {
await Task.Delay(100); // Simulate an async I/O operation required for logging
Console.WriteLine($"{DateTime.Now:HH:mm:ss.fff} {message}");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.