超过最大执行时间

这是我的代码:

  1. 复制电子表格并将其转储到gdrive文件夹(复制数量取决于我的配置文件上的员工数量)。目前,我有800多名员工。
  2. 复制后,gdrive文件夹中的每个电子表格都发布为html。 (我在这里使用了循环)
  3. 获取每个电子表格的pubHTML链接,并将其放入我的配置文件中。

在第3步(获取pubHTML并将其放入配置文件中)时,我得到了超出的最大执行时间。

我的代码按预期工作。我想避免执行错误的最大限制。

    base_model = VGG16(include_top = False,input_shape=(32,32,3))
    base_model.layers.pop() 
    base_model.ouputs =[base_model.layers[-1].output]
    #last = base_model.outputs[0] model.add(base_model) 
    model.add(layers.ZeroPadding2D(padding=(1,1)))
    model.add(layers.Conv2D(filters=1024,kernel_size=(3,3),activation='relu'))
    model.add(layers.GlobalAveragePooling2D())
    model.add(layers.Dense(10,activation='softmax'))

只运行一次代码,不会超过最大执行时间错误。有什么建议吗?

dahom 回答:超过最大执行时间

下面的代码是根据我以前使用的代码修改的。这样的方法可以让您完全执行在execution time limit中无法完成的批处理。通过知道批处理中的一项操作需要多长时间,您可以在达到该限制之前在Properties中保存进度,并在programmatically create a trigger中保存进度以恢复处理。

您需要做一些事情才能使其正常工作:

  1. 您需要修改我的getEmployees_()函数以从您的配置文件中准确地获取员工。
  2. 正如我在评论中提到的那样,您需要重构代码,以便可以对单个员工执行复制。该代码应该放在我的replicateForEmployee_()函数中。
  3. 运行determineTimes()函数以查看获取员工和文件复制所需的时间。
  4. determineTimes()函数中插入replicate()的结果。

determineTimes()的输出将在“执行”页面(“视图”>“执行”)中可见。展开执行行,您将看到如图所示的持续时间。

Execution logs

/**
 * Get all of the employees in the sheet. Assumes row in the sheet is one employee. Adjust the range as needed.
 * @returns {Object[][]}
 */
function getEmployees_() {
  var source = SpreadsheetApp.openById("configfile--xxxx");
  var sheet = source.getSheetByName("Mastersheet");
  return sheet.getRange(1,1,sheet.getLastRow(),sheet.getLastColumn()).getValues();
}

/**
 * Refactor your current code so that this function will replicate all necessary files for a single
 * employee. The function will be called for every single employee.
 * @param {Object[]} employee - The row representing the employee in your config file
 */
function replicateForEmployee_(employee) {
  // Execute your replication process for a SINGLE employee here
}

/**
 * Run this,and then examine your script executions (View > Executions) to see how long each
 * function takes. The resulting durations should replace the corresponding values in the replicate()
 * function (I would recommend rounding up,so 458 --> 500ms).
 */
function determineTimes() {
  console.time("timeToGetEmployees");
  var employees = getEmployees_(); // Get all of the employees
  console.timeEnd("timeToGetEmployees");

  console.time("replicationDuration");
  replicateForEmployee_(employees[0]); // Execute replication once
  console.timeEnd("replicationDuration");
}

/**
 * Replicates the files. When approaching the time limit,will save location and set a trigger to
 * continue from stopping point. Emails user upon completion.
 * @param {Boolean} isFromTrigger
 * @returns {undefined}
 */
function replicate(isFromTrigger) {
  var employees = getEmployees(); // Get all of the employees
  var timeToGetEmployees = 1000; // Assume 1 second to execute getEmployees()
  var replicationDuration = 500; // Number of milliseconds it takes to execute replication for one employee.
  var maxExecutionTime = 360000 - timeToGetEmployees; // 6 mins - time already used to get employees

  var continueFrom = 0; // If first execution,will be zero. Otherwise,get the index to start from.
  if (isFromTrigger === true) {
    continueFrom = parseInt(PropertiesService.getScriptProperties().getProperty("continueFrom"),10);
  }
  var lapsedTime = 0; // ESTIMATED amount of time that has lapsed since the replication started

  for (var i = continueFrom; i < employees.length; i++) {
    // Replicate files for the employee
    replicateForEmployee_(employees[i]);

    // Checks how much time you have left
    lapsedTime += replicationDuration;
    if (lapsedTime >= (maxExecutionTime - replicationDuration)) { // warn just before timing out
      PropertiesService.getScriptProperties().setProperty("continueFrom",i + 1); // save the last iterator value + 1
      createAfterTrigger("replicateFromTrigger",60000); // Start again in one minute
      console.info("About to time out. Last iterator: " + i);
      return;
    }
  }

  deleteAllTriggers(); // Cleanup any lingering triggers
  PropertiesService.getScriptProperties().deleteProperty("continueFrom"); // Clean up properties

  // Send completion email
  MailApp.sendEmail(Session.getEffectiveUser().getEmail(),"Replication Complete","");
}

/**
 * Should ONLY ever be called from a clock trigger.
 * @returns {undefined}
 */
function replicateFromTrigger() {
  replicate(true);
}

/**
 * Create a new trigger to execute the specified function after a certain amount of time.
 * @param {String} functionName - The name of the function to execute
 * @param {Number} milliseconds - The duration (in milliseconds) after the current time when the trigger should run
 * @returns {undefined}
 */
function createAfterTrigger(functionName,milliseconds) {
  ScriptApp.newTrigger(functionName)
  .timeBased()
  .after(milliseconds)
  .create();
}

/**
 * Deletes all triggers in the current project.
 * @returns {undefined}
 */
function deleteAllTriggers() {
  var triggers = ScriptApp.getProjectTriggers();
  for (var i = 0; i < triggers.length; i++) {
    ScriptApp.deleteTrigger(triggers[i]);
  }
}
本文链接:https://www.f2er.com/3120331.html

大家都在问