简体   繁体   English

Azure Functions中同一个线程可以同时运行多个函数吗?

[英]Can the same thread run multiple functions simultaneously in Azure Functions?

All of my functions are static methods.我所有的功能都是 static 方法。 They look like the below sample with a static class and a static method to process the function.它们看起来像下面的示例,使用 static class 和 static 方法来处理 ZC1C425268E683894F145A7。

public static class Function1
{
    [FunctionName("Function1")]
    public static async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
        ILogger log)
    {
        log.LogInformation("C# HTTP trigger function processed a request.");

        await Task.CompletedTask;

        return new OkObjectResult("message");
    }
}

For every function call, I'm setting Thread.CurrentThread.CurrentUICulture based on a request parameter.对于每个 function 调用,我根据请求参数设置Thread.CurrentThread.CurrentUICulture This helps me return localized text based on the culture of the user device.这有助于我根据用户设备的文化返回本地化文本。 (I'm using.resx files) (我正在使用.resx 文件)

My question is, in a highly concurrent environment, is it possible for a user to receive text localized to another culture?我的问题是,在高度并发的环境中,用户是否可以接收本地化到另一种文化的文本? If the same thread processes multiple functions simultaneously, then I believe this issue is possible.如果同一个线程同时处理多个函数,那么我相信这个问题是可能的。 I'm trying to understand if Azure Functions allocates one function call per thread or not.我试图了解 Azure 函数是否为每个线程分配一个 function 调用。

I'm trying to understand if Azure Functions allocates one function call per thread or not我试图了解 Azure 函数是否为每个线程分配一个 function 调用

The important question that I think you want answered is ' Is the same thread allocated for the whole lifetime of a single function call '.我认为您想要回答的重要问题是“是否为单个 function 调用的整个生命周期分配了相同的线程”。 To which the answer is: It depends , which in your case I think is as good as a no.答案是:这取决于,在您的情况下,我认为这与否一样好。

In a non async world, applications flow from top to bottom so there is no chance for the thread to change.在非异步世界中,应用程序从上到下流动,因此线程没有机会改变。 So if you never use the await keyword, you can be pretty sure your function is going to run on a single thread.因此,如果您从不使用await关键字,您可以确定您的 function 将在单线程上运行。

Just using the await keyword doesn't guarantee it'll change threads though.仅使用await关键字并不能保证它会更改线程。 If the async operation has already completed when you call await , it will continue syncronously.如果在调用awaitasync操作已经完成,它将继续同步。 An example of this would be in your example code where you do这方面的一个例子是在你做的示例代码中

await Task.CompletedTask;

Because Task.CompleteTask is already complete, you won't get any thread swapping there.因为Task.CompleteTask已经完成,所以你不会在那里交换任何线程。

On top of this, calling await on a proper asyncronous call doesn't guarantee that the thread will change when it completes.最重要的是,在适当的异步调用上调用await并不能保证线程在完成时会发生变化。 When you call await on a proper async call, it'll release the thread back into the thread pool so it can do other things.当您在适当的异步调用上调用await时,它会将线程释放回线程池,以便它可以做其他事情。 When the async call returns, two different things can happen.当异步调用返回时,可能会发生两种不同的事情。

If there is a SyncronizationContext (There isn't one by default in Azure Functions), it will use that to continue the function on the same thread.如果有一个SyncronizationContext (在 Azure 函数中默认没有一个),它将使用它在同一线程上继续 function。

If there isn't one though, a random thread from the thread pool is used to continue the request.如果没有,则使用线程池中的一个随机线程来继续请求。 It's possible that it could pick the same thread that started it, but that's unlikely.它可能会选择启动它的同一线程,但这不太可能。

So basically in your situation, if you are actually doing async calls in your azure function you cannot rely on the same thread being used throughout the whole of a function call.所以基本上在你的情况下,如果你实际上是在 azure function 中进行异步调用,你不能依赖在整个 ZC1C425268E68385D1AB5074C17A94F14 调用中使用相同的线程。

What should you do你该怎么办

You said you are using Resx files for localization, while I've never had to do this it seems like it's a good way of doing it.您说您正在使用 Resx 文件进行本地化,而我从来没有这样做过,这似乎是一种很好的方法。 I assume you were basing your solution off something like this post .我假设您的解决方案基于类似这篇文章的内容。

While you could manage the threads like this, I think it could potentially lead to unexpected issues if you have multiple Azure Functions inside your Azure Function App.虽然您可以像这样管理线程,但我认为如果您的 Azure Function 应用程序中有多个 Azure 函数,它可能会导致意外问题。 Instead I'd recommend going off Microsoft's recommended localization practices and using the ResourceManager to get the localized resources without changing the thread culture.相反,我建议不要使用Microsoft 推荐的本地化实践,并使用ResourceManager来获取本地化资源,而无需更改线程文化。

*Extra note about Azure Functions *关于 Azure 功能的额外说明

In the comments of the post Rex says Azure Functions run independently from one another. Rex 在帖子的评论中说Azure 函数彼此独立运行。 They each run in their own little sandbox .他们每个人都在自己的小沙箱中运行 This is true, but I think potentially the terminology/naming of things is confusing.这是真的,但我认为事物的术语/命名可能令人困惑。

In Azure, you create a Azure Function App , which contains Azure Function 's. In Azure, you create a Azure Function App , which contains Azure Function 's. I can't find an exact reference for how things work, but to me it seems like the Azure Function App is the part that scales, not the individual Azure Function 's. I can't find an exact reference for how things work, but to me it seems like the Azure Function App is the part that scales, not the individual Azure Function 's. This makes sense because all your individual Functions are part of the same assembly.这是有道理的,因为您所有的个人功能都是同一个程序集的一部分。

So all Azure Function 's inside a single Function App share the same resources, including the thread pool.因此,单个Function App中的所有Azure Function共享相同的资源,包括线程池。 This is why I think changing the thread culture could cause unexpected issues in other Functions.这就是为什么我认为更改线程文化可能会导致其他函数出现意外问题。

If I'm wrong on this though, let me know.如果我在这方面错了,请告诉我。 I was basing what logically makes sense to me and this blog post .我基于对我和这篇博文在逻辑上有意义的内容。 It says ' Functions loaded into memory ' when discussing what happens during a cold start.在讨论冷启动期间发生的情况时,它会显示“已加载到 memory 中的函数”。 I assume if each Function was its own sandbox, they would specify that somewhere.我假设如果每个 Function 都是它自己的沙箱,他们会在某个地方指定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM