本文介绍了一次使用Python初始化的Azure函数应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Azure函数应用与Python结合使用。我有两个功能应用程序,全部使用Postgres DB和Custom Vision。所有功能应用程序都设置为HttpTriggers。现在,当一个函数被触发时,一个新的数据库处理程序(或自定义视觉处理程序)对象将在函数应用程序调用完成后创建,使用和终止。

I'm using Azure function apps with Python. I have two dozen function apps that all use a Postgres DB and Custom Vision. All function apps are setup as HttpTriggers. Right now, when a function is triggered, a new database handler (or custom vision handler) object is created, used and terminated when the function app call is done.

它在传入的每个请求中实例化一个新对象似乎非常适得其反。是否有一种方法可以实例化共享对象一次,然后在调用它们时将其传递给函数?

It seems to be very counterproductive to instantiate a new objects on every single request that comes in. Is there a way to instantiate shared objects once and then pass them to a function when they are called?

推荐答案

通常,Azure Functions旨在为无状态,并且不会在一次调用中共享对象下一个。但是,也有一些例外。

In general, Azure Functions are intended to be stateless and not share objects from one invocation to the next. However, there are some exceptions.

共享连接对象

Azure文档推荐,用于共享打算在应用程序中一次打开并一次又一次使用的连接对象。

Azure Docs recommend the Improper Instantiation Pattern for sharing of connection objects that are intended in an application to be opened once and used again and again.

需要牢记一些注意事项为此,主要是:

There are some things to keep in mind for this to work for you, mainly:

他们在那里有一些演练可能会帮助您。由于您的问题相当笼统,因此我最好的建议是建议您通读它,看看是否会对您有所帮助。

They have some walkthroughs there that will probably help you. Since your question is fairly generic, the best I can do is recommend you read through it and see if that will help you.

持久功能

另一种方法是考虑代替标准。它们旨在能够在函数之间传递对象,从而使它们并非完全无状态。

The alternative is to consider the Durable Functions instead of the standard. They are intended to be able to pass objects between functions making them not quite stateless.

这篇关于一次使用Python初始化的Azure函数应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 14:40