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

查看:59
本文介绍了多线程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?

推荐答案

在任何环境(包括节点和python)中,Cloud Functions中目前都没有线程.您不应该依赖于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天全站免登陆