0

I implemented a Web App but it takes around 30 seconds to complete the actions before sending the data to the spreadsheet. In the code I changed huge text for a Utilities.sleep(30000) to make it easier to see and get the same result:

function doGet(e) {
  const lock = LockService.getDocumentLock();
  if (lock.tryLock(360000)) {
    try {
      var backteam = e.parameter.backteam;
      var backodds = e.parameter.backodds;
      var layteam = e.parameter.layteam;
      var layodds = e.parameter.layodds;
      var vantagem = e.parameter.advantage;

      Utilities.sleep(30000)

      var second_sheet = SpreadsheetApp.openById('XXXXXXX');
      var second_sheet_page = second_sheet.getSheetByName('STACKTEST');
      var r = 1;
      while (second_sheet_page.getRange(r, 1).getValue()) {
        r++;
      }
      second_sheet_page.getRange(r, 1, 1, 5).setValues([[backteam,backodds,layteam,layodds,advantage]]);
    } catch (e) {
      //pass
    } finally {
      lock.releaseLock();
    }
  } else {
    //pass
  }
}

In my Python code:

import requests

backteam = "Vasco"
backodds = "3.00"
layteam = "Flamengo"
layodds = "1.50"
advantage = "25.55"
webAppsUrl = "https://script.google.com/macros/s/XXX/exec"

requests.get(webAppsUrl + "?backteam=" + backteam + "&backodds=" + backodds + "&layteam=" + layteam + "&layodds=" + layodds + "&advantage=" + advantage)

I wouldn't want to wait for the end of execution to continue with the other tasks my Python does (I know I can force a timeout on the request, but then I'm not sure if the Web App received the request).

Is there a way to not have to wait doing something in Google Apps Script?

TheMaster
  • 45,448
  • 6
  • 62
  • 85
Digital Farmer
  • 1,705
  • 5
  • 17
  • 67
  • I have to apologize for my poor English skill. Unfortunately, I cannot understand `I wouldn't want to wait for the end of execution to continue with the other tasks my Python does (I know I can force a timeout on the request, but then I'm not sure if the Web App received the request). Is there a way to not have to wait doing something in Google Apps Script?`. – Tanaike Jul 02 '22 at 05:03
  • And also, I cannot understand `I wouldn't want to wait for the end of execution to continue with the other tasks my Python does (I know I can force a timeout on the request, but then I'm not sure if the Web App received the request).` and `Is there a way to not have to wait doing something in Google Apps Script?`. Can I ask you about the detail of your question and your goal? – Tanaike Jul 02 '22 at 05:03
  • There's also probably a Python duplicate somewhere, which can use `async` on `requests.get` call. So, I added the tag. – TheMaster Jul 02 '22 at 09:11
  • Try linked `````httpx`````, if you don't want triggers on GAS. – TheMaster Jul 02 '22 at 09:16
  • In other words @Tanaike , I don't need a response in my request, I just want to activate the script in GAS and continue the other tasks of the Python code. I just want to let the GAS know that it should run the script, what will happen during execution does not interest me.. – Digital Farmer Jul 02 '22 at 11:46
  • In my case @TheMaster , ```async``` + ```requests``` wouldn't solve because it would keep waiting in the background for the end of the google apps script execution, understand? – Digital Farmer Jul 02 '22 at 11:46
  • Add @TheMaster → I'll analyze the operation of the Trigger generation and the use of ```httpx``` and return saying if it solved, thank you! – Digital Farmer Jul 02 '22 at 11:47
  • I installed ```httpx``` and ran the tests @TheMaster , but it kept waiting for the entire GAS script to run in the background, from what I understand from the module, it doesn't solve the case. – Digital Farmer Jul 02 '22 at 12:04
  • `understand`. Not really. Does it matter, if it waits in the background? You can still do `other tasks my Python does` in the foreground. – TheMaster Jul 02 '22 at 12:04
  • @TheMaster yes, it matters because I use multiprocessing, so running in the background forces usage and would be more expensive ($) at the end of the month. The trigger maybe will solve, I just need to look at the quotas limit of tiggers created daily, because each requests sent will be a new trigger. – Digital Farmer Jul 02 '22 at 12:09
  • There are quotas, but you'll basically be deleting the trigger within 30s of execution. – TheMaster Jul 02 '22 at 12:10
  • @TheMaster Now my question is, how do I create this trigger with the data that will be sent to the spreadsheet? I'm reading the documentation and I couldn't understand how to generate the trigger with values. – Digital Farmer Jul 02 '22 at 12:15
  • Better to ask follow up questions as a new post, but you can use Cache or Properties with timestamp or a uuuid as key. – TheMaster Jul 02 '22 at 12:18
  • Thank you for replying. When I saw your question for the first time, I thought that "timeout" of request will be useful. But, from `(I know I can force a timeout on the request, but then I'm not sure if the Web App received the request)`, I understood that this was not useful for your situation. In the current stage, how about using "Thread"? By this, for example, first, just after request to Web Apps, you can run another script on the python side. But, I'm not sure whether this is your expected result. I apologize for this. – Tanaike Jul 03 '22 at 00:01
  • Or, as a simple method, when OnChange trigger is used for executing your Google Apps Script, only when a sample value is put to a cell, Google Apps Script is run with the asynchronous process. I think that by this method, your goal can be achieved. – Tanaike Jul 03 '22 at 00:04
  • Hello @Tanaike , we currently have a special use option for ```requests``` that puts a ```timeout``` for ```connect``` and a different ```timeout``` for ```read```, so I can break this wait as soon as I confirm the connection, that way I don't have to wait for the end of execution, in case you want to see the that we commented: https://stackoverflow.com/questions/72839903/triggers-maintaining-an-activation-sequence-according-to-their-creation-and-prot – Digital Farmer Jul 03 '22 at 00:19
  • This option answeared to @TheMaster → https://stackoverflow.com/a/72840366/11462274 – Digital Farmer Jul 03 '22 at 00:21
  • Thank you for replying. from `I know I can force a timeout on the request, but then I'm not sure if the Web App received the request` in your question, I had thought that you don't want to use the timeout of the request. But, I noticed that my understanding was not correct. I deeply apologize for my poor English skill. – Tanaike Jul 03 '22 at 02:49

0 Answers0