多线程 Firebase 功能 [英] Multi-Thread Firebase function

查看:38
本文介绍了多线程 Firebase 功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在开发一个 Firebase 函数,它接受来自用户的请求并更新这些用户正在收听的分支下的几个节点.

So I am developing a Firebase function that accepts requests from users and updates few nodes under a branch these users are listening to.

我的问题是,如果函数同时接收到两个客户端请求,则会触发两个函数同时执行和更新数据.

My issue is if the function receives two client requests at the same time, that triggers two functions to execute and update the data at the same time.

我知道这通常可以通过事务解决,但我的更新是在各个节点上完成的,而不仅仅是一个值(即计数器).

I know this is typically solved by a transaction, but my updates are done on various nodes not only one value (i.e counter).

在传统的多线程编程中,这个问题是通过锁定代码的执行来解决的,因此它只能由一个线程执行,当当前线程完成时,下一个线程会继续执行.

In a traditional multi-threading programming, this problems is solved by locking the code from executing so it can only be executed by one thread, where the next one resumes when the current one finishes.

这是 Firebase Functions 中的一个选项吗?如果可以,怎么做?

Is this an option in Firebase Functions? If so, how can it be done?

推荐答案

目前Cloud Functions在任何环境下都没有线程,包括node和python.您不应该依赖 Cloud Function 中的进程级别锁定 - 使用数据库事务来确保更新是原子的和一致的.您的每个函数调用都将彼此完全隔离.

There is currently no threading in Cloud Functions in any environment, both node and python included. You should not be depending on process level locking in Cloud Function - use a database transaction to ensure that updates are atomic and consistent. Each of your function invocations is going to be completely isolated from each other.

这篇关于多线程 Firebase 功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆