我想知道信号量(锁)是否可以在 Azure 函数中工作。 我不希望两个单独的网络作业同时运行。网络作业采用相同的应用服务计划。
这是我可以用信号量保证的吗? (因为这可以实现跨进程锁定?)
最佳答案
第一个问题:您谈论的是 Functions 和 WebJobs。是哪一个?
如果您的应用服务计划进行任何扩展,信号量将不起作用,因为两个实例可能会在两台不同的计算机上启动。好处是:(对于 WebJobs)有一个简单的解决方案。
[Singleton]
public static async Task ProcessImage([BlobTrigger("images")] Stream image)
{
// Process the image
}
In this example, only a single instance of the ProcessImage function will run at any given time. When the function is triggered by a new image being added to the images container, the runtime will first attempt to acquire the lock (blob lease). Once acquired, the lock is held (and the blob lease is renewed) for the duration of the function execution, ensuring no other instances will run. If another function instance is triggered while this function is running it will wait for the lock, periodically polling for it.
您可以在这里找到更多信息:Azure WebJobs SDK - Singleton
编辑:
如果您使用的是 Azure Functions:基于 TimerTrigger 运行的函数 seem to run as Singletons .
The timer trigger supports multi-instance scale-out. A single instance of a particular timer function is run across all instances.
关于azure - Azure Web 作业中的信号量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47280923/