Forum Discussion

arvindnsn's avatar
arvindnsn
Contributor
4 years ago
Solved

Delay or wait process

Hello,

I have a requirement where a snap has to wait for n amount of seconds before it executes. The n amount of seconds varies based on the output of the previous snap. How do I carry the output of a variable from a previous snap and pass it as the number of seconds to wait for the following snap.

Thanks
Aravind N

  • Yes there is. In the incoming object itself, map a field called “time” for example. Than, in the script, right after the
    in_doc=self.input.next() line, add the following one:

    sleep_time = in_doc.get('time')

    sleep_time is your variable you can use to dynamically change the waiting time for every document. Meaning you need to change the sleep function to:

    time.sleep(sleep_time)

    You can manipulate the waiting time by changing the “time” value in the mapper before the script, or even pass that value from a parameter.

7 Replies

    • arvindnsn's avatar
      arvindnsn
      Contributor

      Is there a way to send a variable from the previous snap to the script Snap instead of hardcoding the seconds to sleep.

      The below script has time.sleep(20), instead can we pass a parameter like time.sleep($sleep_time), where $sleep_time carries the number of seconds from the previous mapper.

      def execute(self):
      self.log.info(“Executing Transform script”)
      while self.input.hasNext():
      try:
      # Read the next input document, store it in a new dictionary, and write this as an output document.
      inDoc = self.input.next()
      time.sleep(20)
      outDoc = inDoc
      self.output.write(inDoc, outDoc)

      • bojanvelevski's avatar
        bojanvelevski
        Valued Contributor

        Yes there is. In the incoming object itself, map a field called “time” for example. Than, in the script, right after the
        in_doc=self.input.next() line, add the following one:

        sleep_time = in_doc.get('time')

        sleep_time is your variable you can use to dynamically change the waiting time for every document. Meaning you need to change the sleep function to:

        time.sleep(sleep_time)

        You can manipulate the waiting time by changing the “time” value in the mapper before the script, or even pass that value from a parameter.

    • arvindnsn's avatar
      arvindnsn
      Contributor

      Is there a way to send a variable from the previous snap to the script Snap instead of hardcoding the seconds to sleep.

      below script says time.sleep(120), instead can we pass a parameter like time.sleep($sleep_time), where $sleep_time carries the number of seconds from the previous mapper.

    • arvindnsn's avatar
      arvindnsn
      Contributor

      The second one seems to be shorter and i was able to provide variables to sleep time instead of hardcoding the seconds. Thanks you!