Google Apps脚本:多个用户点击doGet和doPost的脚本,变量会混淆吗? [英] Google Apps Script: Multiple users hitting script for doGet and doPost, can variables get mixed up?

查看:103
本文介绍了Google Apps脚本:多个用户点击doGet和doPost的脚本,变量会混淆吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用GAS文件作为我正在构建的应用程序的服务器端.我定义了doPost(e)和doGet(e)函数,例如:

I'm using a GAS file as my server side for an app I'm building. I have doPost(e) and doGet(e) functions defined, for example:

function doPost(e) {
     var postData = e.postData.contents;
     var jsonRaw = JSON.parse(postData); 
     var name = jsonRaw.name;

     // make a long URLFetch call...
     
     textOutput = ContentService.createTextOutput('execution done for ' + name); 
     return textOutput         
   }

我没有任何全局"在特定功能范围之外声明的变量.我的问题是,如果我为用户A运行了长时间运行的doPost,并且突然用户B在用户A的请求完成之前也发出了doPost请求,是否有可能会重新分配名称变量并且用户A在输出中看到用户B的名称?

I do not have any "global" variables declared outside of a specific function's scope. My question is, if I have a long-running doPost running for user A and suddenly user B also makes a doPost request before user A's request completes, is it possible that the name variable gets reassigned and user A sees user B's name in the output?

根据此处的答案:全局变量值不能多次使用功能,我相信我会没事的,但我想确认一下.谢谢!

Based on the answer here: Global Variable value not usable in multiple functions I believe I should be fine, but I wanted to confirm. Thanks!

推荐答案

局部变量

局部变量不是共享资源.调用函数时,在函数内部定义的变量将属于该函数的作用域.这意味着,如果您有一个将参数存储在变量中的函数,则此变量仅对特定的函数执行有效.

Local Variables

Local variables are not a shared resource. When calling a function the variables you defined inside the function will belong to that function's scope. This means that if you have a function that stores a parameter in a variable this variable will be valid only for this particular function execution.

全局变量当然是不同的,因为它们的值对于Project中的所有函数都是已知的.这些是共享资源,因此如果您需要修改它们的值,则希望锁定对它们的访问.

Global variables are of course different since their values are known to all the functions in your Project. These are shared resource so you would want to lock the access to them if you need to modify their value.

您可以创建一个简单的Web应用程序来验证此行为,该应用程序获取查询参数,将其存储在变量中,并在10秒钟后返回.

You can verify this behavior creating a simple Web App that gets a query parameter, stores it in a variable and after 10 seconds returns it.

function doGet(e) {
  var param = e.queryString;
  Utilities.sleep(10000);
  return ContentService.createTextOutput(param);
}

如果在10秒的窗口内使用不同的参数调用此方法,则应注意第一个GET请求返回的值是第二个请求的查询参数.但这不是.

If you call this method with different parameters within the 10 seconds window, you should observe that the value returned by the first GET request is the query parameter of the second one. But it's not.

锁定服务

应用脚本网络应用

这篇关于Google Apps脚本:多个用户点击doGet和doPost的脚本,变量会混淆吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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