ecFlow's documentation is now on readthedocs!

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 8 Next »

Here is an example of a script as a local native python task:

There are several ways of doing this. The following are examples.

  •  There are two ways of accessing the ecflow child commands(init,event,meter, label,abort,complete). 
     We can either call the child commands directly using ecflow extension, or we can call system command to access ecflow_clent
     The following examples will use ecflow python extension. However this requires that PYTHONPATH is set directory where ecflow.so extension was installed.
  • definition file:
    The default ECF_MICRO is %, this may interfere with your python scripts. In this case you either redefine it, in the task definition or directly in the python script.
    task python
      edit ECF_MICRO '$'
      edit ECF_JOB_CMD 'python $ECF_JOB$ > $ECF_JOBOUT$ 2>&1'
      label info "none"
      meter step -1 100 100
      event 1
      event 2
  • Header

    python header
    import os, time, signal, sys
    import ecflow
     
    print "PYTHONPATH====================================================="
    print os.environ['PYTHONPATH'].split(os.pathsep)
    class Client(object):
       def __init__(self):
          print "Creating Client"
          self.ci = ecflow.Client()
          self.ci.set_host_port("%ECF_NODE%","%ECF_PORT%")
          self.ci.set_child_pid(os.getpid())
          self.ci.set_child_path("%ECF_NAME%")
          self.ci.set_child_password("%ECF_PASS%")
          self.ci.set_child_try_no(%ECF_TRYNO%)
       
          print "Only wait 20 seconds, if the server cannot be contacted (note default is 24 hours) before failing"
          self.ci.set_child_timeout(20) 
        
          signal.signal(signal.SIGINT,  self.signal_handler) 
          signal.signal(signal.SIGHUP,  self.signal_handler)
          signal.signal(signal.SIGQUIT, self.signal_handler)
          signal.signal(signal.SIGILL,  self.signal_handler)
          signal.signal(signal.SIGTRAP, self.signal_handler)
          signal.signal(signal.SIGIOT,  self.signal_handler)
          signal.signal(signal.SIGBUS,  self.signal_handler)
          signal.signal(signal.SIGFPE,  self.signal_handler)
          signal.signal(signal.SIGUSR1, self.signal_handler)
          signal.signal(signal.SIGUSR2, self.signal_handler)
          signal.signal(signal.SIGPIPE, self.signal_handler)
          signal.signal(signal.SIGTERM, self.signal_handler)
          signal.signal(signal.SIGXCPU, self.signal_handler)
          signal.signal(signal.SIGPWR,  self.signal_handler)
        
       def signal_handler(self,signum, frame):
          print 'Aborting: Signal handler called with signal ', signum
          self.ci.child_abort("Signal handler called with signal " + str(signum));
        
       def __enter__(self):
          self.ci.child_init()
          return self.ci
        
       def __exit__(self,ex_type,value,tb):
          print "Client:__exit__: ex_type:" + str(ex_type) + " value:" + str(value) + "\n" + str(tb)
          if ex_type != None:
             self.ci.child_abort("Aborted with exception type " + str(ex_type) + str(value))
          self.ci.child_complete()
          return False

    This header will automatically handle calling the child command init() and complete(), it will also handle any signals, and exceptions. In which case it call abort automatically

    task wrapper:

    $include <head.py>
    $manual
       This is the manual section. Instead of calling python from the ECF_JOB_CMD we could alternatively place,
           #!/bin/env/python
       on the first line of this file.
    $end
    
    $comment
       Note: We do not need a tail,
    $end
     
    # This will also handle call to sys.exit(), i.e Client.__exit__ will still be called.
    with Client() as ci:
       for i in range(1,100):
          ci.child_meter('step',i)
          ci.child_label('info', "value_" + str(i))
          time.sleep(1)
          
       ci.child_event('event_fred')
       print "Finished event,meter and label child commands"
  •  


     


 

  • No labels