如何配置 Unity 进行类型/模块的异步初始化

发布于 2024-12-01 21:12:22 字数 731 浏览 1 评论 0原文

如何设置 Unity,以便类可以异步初始化,而不会阻止其他模块的加载(仅阻止明确需要异步类型实例的其他类型)?我正在考虑的类是引用数据缓存,它从数据库中提取常用数据的快照,并且我需要在让任何其他模块访问它之前完成预缓存(如果请求在我的类中被阻止)将快速阻止主线程并阻止所有其他模块初始化)。这变得更加重要,因为我有多个这样的引用数据类

作为一个例子,假设我有一个这样的类:

public class ProductCache{

    public ProductCache(){}

    public Initialize(){
        // a very slow DB call to fetch frequently used products
        Thread.Sleep(30*1000);
    }

    public Product FindProduct(string productDescription){
        /* check cache, if not there try the db */
    }
}

如果我从构造函数调用Initialize,我将阻塞调用它的线程(从Unity)30秒,阻止我并行创建其他(类似)类。如果我只是将任务放在线程池上,Unity 最终将到达需要我的产品缓存的另一个类正在执行其初始化代码,然后它会访问尚未完全初始化的数据结构(在这种情况下,会导致在缓存未命中和调用数据库来获取特定产品的情况下,30 秒内可能会有很多此类请求)

谢谢 奥斯卡

How do I set up Unity such that a class can intialize asynchronously without blocking the load of other modules (only blocking other types that explicitly need an instance of the asynch type)? The kind of classes I'm thinking of are reference data caches that pull in a snapshot of frequently used data from the database, and I need to finish precaching before I let any other modules access it (if the requests are blocked within my class I will quickly hold up the main thread and block all other modules from initializing). This becomes more important as I have multiple such reference data classes

As an example, say I have a class like this:

public class ProductCache{

    public ProductCache(){}

    public Initialize(){
        // a very slow DB call to fetch frequently used products
        Thread.Sleep(30*1000);
    }

    public Product FindProduct(string productDescription){
        /* check cache, if not there try the db */
    }
}

If I call Initialize from the constructor I will block the thread that invokes it (from Unity) for 30 seconds, preventing me from creating other (similar) classes in parallel. If I simply put the task on the threadpool Unity will eventually get to the point where another class that needs my product cache is executing its initialization code, and it is then accessing datastructures that aren't fully initialized yet (in this case it would result in a cache miss and a call to the db to fetch the particular product, and there might be many such requests in 30 seconds)

thanks
Oskar

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你列表最软的妹 2024-12-08 21:12:22

您需要列出正在运行的任务,并行执行它们,并使用 Task.WaitAll() 等待它们完成后再继续。

在 .Net 4 中,这应该可行,并且错误处理变得容易:

public void InitializeAll()
{
    List<Task> initTasks = new List<Task>();

    ProductCache productCache = new ProductCache();
    SomeOtherCache someOtherCache = new SomeOtherCache();

    initTasks.Add(Task.Factory.StartNew(() => productCache.Initialize()));
    initTasks.Add(Task.Factory.StartNew(() => someOtherCache.Initialize()));

    try
    {
        Task.WaitAll(initTasks.ToArray());
    }
    catch (AggregateException ex)
    {
        Console.WriteLine("Oh dear!");
    }
}

在老式 .Net 中,尝试这个。我假设您为每个可初始化对象使用一个接口,并且我省略了错误处理:

public void InitializeAll(IEnumerable<IInitializable> initializeUs)
{
    List<WaitHandle> handles = new List<WaitHandle>();

    foreach(IInitializable init in initializeUs)
    {
        // Make a copy of the reference in the local scope
        IInitializable temp = init;

        ManualResetEvent done = new ManualResetEvent(false);
        handles.Add(done);

        ThreadPool.QueueUserWorkItem(delegate
        {
            try
            {
                temp.Initialize();
            }
            finally
            {
                done.Set();
            }
        });
    }

    // Wait for all the handles to be set
    WaitHandle.WaitAll(handles.ToArray());
}

You need to make a list of the running tasks, execute them in parallel, and use Task.WaitAll() to wait for them to complete before continuing.

In .Net 4, this should work, and error handling is made easy:

public void InitializeAll()
{
    List<Task> initTasks = new List<Task>();

    ProductCache productCache = new ProductCache();
    SomeOtherCache someOtherCache = new SomeOtherCache();

    initTasks.Add(Task.Factory.StartNew(() => productCache.Initialize()));
    initTasks.Add(Task.Factory.StartNew(() => someOtherCache.Initialize()));

    try
    {
        Task.WaitAll(initTasks.ToArray());
    }
    catch (AggregateException ex)
    {
        Console.WriteLine("Oh dear!");
    }
}

In old-school .Net, try this. I've assumed you're using an interface for each initializable object, and I've left out error handling:

public void InitializeAll(IEnumerable<IInitializable> initializeUs)
{
    List<WaitHandle> handles = new List<WaitHandle>();

    foreach(IInitializable init in initializeUs)
    {
        // Make a copy of the reference in the local scope
        IInitializable temp = init;

        ManualResetEvent done = new ManualResetEvent(false);
        handles.Add(done);

        ThreadPool.QueueUserWorkItem(delegate
        {
            try
            {
                temp.Initialize();
            }
            finally
            {
                done.Set();
            }
        });
    }

    // Wait for all the handles to be set
    WaitHandle.WaitAll(handles.ToArray());
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文