cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

Delaying a process

Abhishek_117
New Contributor II

How can i delay a process according to our needs, is there any snap to perform wait operation?
I tried with script snap, mentioned there to delay for 15 seconds,but its taking more time than 15 seconds.

CODE

 

# Import the interface required by the Script snap.
from com.snaplogic.scripting.language import ScriptHook
from random import randint
from time import sleep
import time

class TransformScript(ScriptHook):
def __init__(self, input, output, error, log):
self.input = input
self.output = output
self.error = error
self.log = log

# The "execute()" method is called once when the pipeline is started
# and allowed to process its inputs or just send data to its outputs.
def execute(self๐Ÿ˜ž
self.log.info("Executing Transform script")
while self.input.hasNext():
data = self.input.next()
time.sleep(15)
map = {"out": data}
self.output.write(map)

self.log.info("Script executed")

# The "cleanup()" method is called after the snap has exited the execute() method
def cleanup(self๐Ÿ˜ž
self.log.info("Cleaning up")

# The Script Snap will look for a ScriptHook object in the "hook"
# variable. The snap will then call the hook's "execute" method.
hook = TransformScript(input, output, error, log)
 
 
 
This code should perform 15 seconds of wait operation,but taking long time.

 

1 ACCEPTED SOLUTION

j_angelevski
Contributor III

Hi @Abhishek_117 

The code you shared does wait 15 seconds, but it waits on each input document. This means that if you have 10 input documents, you will have a wait time of 150 seconds.

I am not sure where you set up your script in your pipeline, but you have to make sure you are either waiting 15 seconds before the input documents start processing, or waiting 15 seconds after each input document has been processed. You can do this if you amend your code, specifically the time.sleep() function.

If you want your script to wait for 15 seconds before it starts processing the input documents, the you can use the following code in your execute() function:

def execute(self):
        self.log.info("Executing Transform script")
        time.sleep(15)
        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()
                outDoc = {
                    'original' : inDoc
                }
                self.output.write(inDoc, outDoc)
            except Exception as e:
                errDoc = {
                    'error' : str(e)
                }
                self.log.error("Error in python script")
                self.error.write(errDoc)

        self.log.info("Script executed")

 Or, if you want to wait for 15 seconds after all documents have been 

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()
                outDoc = {
                    'original' : inDoc
                }
                self.output.write(inDoc, outDoc)
            except Exception as e:
                errDoc = {
                    'error' : str(e)
                }
                self.log.error("Error in python script")
                self.error.write(errDoc)

        time.sleep(15)
        self.log.info("Script executed")

The only difference is that you need to put time.sleep(15) before the while loop or after the while loop, this of course depends on your case.

View solution in original post

5 REPLIES 5

Abhishek_117
New Contributor II

I sincerely appreciate your excellent explanation @j_angelevski ,Your contribution has been truly valuable. Thank you very much!