Google App脚本和公共锁 [英] Google App Script and public locks

查看:76
本文介绍了Google App脚本和公共锁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个电子表格,其中有一个 doGet(),一个 doPost()以及一个定时触发器来进行一些更新.我希望这些互相阻止.是的,希望是LockService ...

I have a spreadsheet that I have a doGet() a doPost() as well as a timed trigger to do some updates. I want these to block each other. Yay the LockService... hopefully.

LockService 上的Google文档未记录 getPublicLock()方法,我似乎找不到适合自己需求的任何东西.上面的文档确实说过, getDocumentLock()将在不是特别有用的Web应用程序中返回 null ,互联网上的一般帖子建议它会停止一些代码正在同时运行...这不是我想要的.我想要的是在更新数据时停止对数据的请求.

The Google documentation on the LockService does not document the getPublicLock() method and I can't seem to find anything specific to my needs. The documentation above does say that getDocumentLock() will return null in a web app which is not particularly useful, and general posts around the interweb suggest that it will stop bits of code being run concurrently... which isn't quite what I want. What I want is to stop requests for data when data is being updated.

我已尝试对其进行测试,但似乎无法让脚本运行足够长的时间来对其进行满意的测试.

I have tried to test it, but I can't seem to get a script to run for suitably long enough to test this to my satisfaction.

如果我使用:

var lock = LockService.getPublicLock();
lock.waitLock(30000);

在所有3个入口点中,如果调用任何其他调用,脚本会成功锁定吗?

In all 3 entry points, will the script successfully lock when any other invocation is called?

推荐答案

我还没有在Google-Apps-Script中遇到LockService.getPublicLock().

I have not run into LockService.getPublicLock() in Google-Apps-Script.

但是LockService.getScriptLock()应该可以工作.如文档所述:

However the LockService.getScriptLock() should work. As the documentation states :

获取一个锁定,该锁定可防止任何用户同时运行一段代码.无论用户的身份如何,都无法同时执行由脚本锁保护的代码段.请注意,直到调用Lock.tryLock(timeoutInMillis)或Lock.waitLock(timeoutInMillis)才真正获得该锁.

Gets a lock that prevents any user from concurrently running a section of code. A code section guarded by a script lock cannot be executed simultaneously regardless of the identity of the user. Note that the lock is not actually acquired until Lock.tryLock(timeoutInMillis) or Lock.waitLock(timeoutInMillis) is called.

我特别在Web App中使用了它,据我测试,它可以按预期工作:

I have used it particularly with Web App and as far as I have tested it it works as promised:

尽管使用该应用程序的任何人都在脚本中保留了一个脚本锁,但是对于任何用户,由脚本锁锁定的任何其他块都将不会执行.

While anyone using the app has reserved a single Script Lock in the script, any other block locked by Script Lock will not execute, for any user.

本来可以将此添加为评论,但我无权发表评论.

I would have added this as a comment but I have no right to comment yet..

这篇关于Google App脚本和公共锁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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