此修改是否使我的方法 运行 异步
Does this modification makes my method run asynchronously
以下是记录器调用,这是阻塞的:
public static void Info(Category category, SubLevel subLevel, object message)
{
CommonLogger?.Info($"{category}, {(int) subLevel}, {message}");
}
我想让它异步执行,因此我做了以下修改,因为根据我的理解,使用 Async-Await for void return 方法,就像当前的方法一样不是一个好的策略:
public static void InfoAsync(Category category, SubLevel subLevel, object message)
{
Task.Run(() => CommonLogger?.Info($"{category}, {(int)subLevel}, {message}"));
}
代码上下文-
- 使日志记录运行异步
- 即发即弃,主要业务逻辑分开进行
我的问题是:
我进行此更改是否正确?
还有更好的方法吗?
- 是的,现在 是 运行 异步的(从某种意义上说,您的方法 return 没有等待日志记录完成,不是这个意思由 Yuval Itzchakov 解释。
好吧,你为什么不想将该方法更改为 return Task
并使其也可供调用者等待:
public static async Task InfoAsync(Category category, SubLevel subLevel, object message)
{
await Task.Run(() => CommonLogger?.Info($"{category}, {(int)subLevel}, {message}"));
}
但正如您和其他评论者所指出的:如果您不需要它,它只会增加不必要的复杂性(例如,编译器构建了一个不必要的状态机)。
一般来说,要回答 "can it be done better" 这样的问题,您需要了解 "better" 的标准。这取决于 如何 以及在 什么条件下 这将被调用以及要求是什么。
是的,这现在是一种 ASync 方法,但是,其他两个不是正确的问题(如 JayMee 所述)。它们纯粹是人们发展的方式。例如,我会这样做,但是,Joe Bloggs 可能不会。
希望对您有所帮助。
Does this modification makes my method run asynchronously?
不,不是。 Concurrency and parallelism are two different concepts。人们倾向于使并发和并行可以互换,但事实并非如此。您的代码将 运行 并行 到其他执行单元,但操作本身并不是 异步 。如果你真的想进行异步操作,你会使用异步 IO 来写入底层记录器。
Am I correct in making this change?
我会说不。这叫做 async over sync anti-pattern (see the link for an extensive explanation of what that means). I would avoid this pattern, as it creates a false sense of asynchrony. Your method isn't really naturally asynchronous, true asynchronous methods don't need to use extra thread-pool threads.
Is there still a better way, to do the same?
是的,不要这样做。让此方法的调用者显式调用 Task.Run
以便他们知道所述委托将在线程池上执行。或者更好的是,将 async IO 与记录器实现一起使用。毕竟,我假设这可能会写入底层文件或执行某种网络操作以通过网络发送日志。如果是这样,请改用真正的异步 API。
以下是记录器调用,这是阻塞的:
public static void Info(Category category, SubLevel subLevel, object message)
{
CommonLogger?.Info($"{category}, {(int) subLevel}, {message}");
}
我想让它异步执行,因此我做了以下修改,因为根据我的理解,使用 Async-Await for void return 方法,就像当前的方法一样不是一个好的策略:
public static void InfoAsync(Category category, SubLevel subLevel, object message)
{
Task.Run(() => CommonLogger?.Info($"{category}, {(int)subLevel}, {message}"));
}
代码上下文-
- 使日志记录运行异步
- 即发即弃,主要业务逻辑分开进行
我的问题是:
我进行此更改是否正确?
还有更好的方法吗?
- 是的,现在 是 运行 异步的(从某种意义上说,您的方法 return 没有等待日志记录完成,不是这个意思由 Yuval Itzchakov 解释。
好吧,你为什么不想将该方法更改为 return
Task
并使其也可供调用者等待:public static async Task InfoAsync(Category category, SubLevel subLevel, object message) { await Task.Run(() => CommonLogger?.Info($"{category}, {(int)subLevel}, {message}")); }
但正如您和其他评论者所指出的:如果您不需要它,它只会增加不必要的复杂性(例如,编译器构建了一个不必要的状态机)。
一般来说,要回答 "can it be done better" 这样的问题,您需要了解 "better" 的标准。这取决于 如何 以及在 什么条件下 这将被调用以及要求是什么。
是的,这现在是一种 ASync 方法,但是,其他两个不是正确的问题(如 JayMee 所述)。它们纯粹是人们发展的方式。例如,我会这样做,但是,Joe Bloggs 可能不会。
希望对您有所帮助。
Does this modification makes my method run asynchronously?
不,不是。 Concurrency and parallelism are two different concepts。人们倾向于使并发和并行可以互换,但事实并非如此。您的代码将 运行 并行 到其他执行单元,但操作本身并不是 异步 。如果你真的想进行异步操作,你会使用异步 IO 来写入底层记录器。
Am I correct in making this change?
我会说不。这叫做 async over sync anti-pattern (see the link for an extensive explanation of what that means). I would avoid this pattern, as it creates a false sense of asynchrony. Your method isn't really naturally asynchronous, true asynchronous methods don't need to use extra thread-pool threads.
Is there still a better way, to do the same?
是的,不要这样做。让此方法的调用者显式调用 Task.Run
以便他们知道所述委托将在线程池上执行。或者更好的是,将 async IO 与记录器实现一起使用。毕竟,我假设这可能会写入底层文件或执行某种网络操作以通过网络发送日志。如果是这样,请改用真正的异步 API。