ecFlow's documentation is now on readthedocs!

Here is an example of a script as a local native Python task: This example shows that we can still take advantage of pre-processing and variable substitution. This will work on the manual and comments section, these will also be pre-processed.

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_client
    The following examples will use the ecFlow Python extension. This requires that the PYTHONPATH is set to the directory where 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_EXTN .py.  # search for
          edit ECF_MICRO '$'
          edit ECF_JOB_CMD 'python $ECF_JOB$ > $ECF_JOBOUT$ 2>&1'
          label info "none"
          meter step -1 100 100
          event my_event

    Notice that the ECF_JOB_CMD calls Python. This allows us to change the Python version, within the viewer. Alternatively it can be omitted, providing we add "#!/usr/bin/env python" as the first line of our Python script.

  • Header:

    python header
    # Name        :
    # Author      : Avi
    # Revision    : $Revision: #10 $
    # Copyright 2009- ECMWF.
    # This software is licensed under the terms of the Apache Licence version 2.0
    # which can be obtained at
    # In applying this licence, ECMWF does not waive the privileges and immunities
    # granted to it by virtue of its status as an intergovernmental organisation
    # nor does it submit to any jurisdiction.
    import datetime,time
    import os, signal, platform
    import ecflow
    print("cwd: " + os.getcwd())
    except KeyError:
        print("Could not get PYTHONPATH")
    except KeyError:
        print("Could not get LD_LIBRARY_PATH")
    class Client(object):
        """Encapsulate communication with the ecflow server. This will automatically call
           the child command init()/complete(), for job start/finish. It will also
           handle exceptions and signals, by calling the abort child command.
           *ONLY* one instance of this class, should be used. Otherwise zombies will be created.
        def __init__(self,init_add_vars = False):
            print ("Creating Client")
   = ecflow.Client()
            if init_add_vars:
      {"name1":"1", "name2":"2"})
      [ecflow.Variable("name3","3"), ecflow.Variable("name4","4")])
            print("   Only wait 20 seconds, if the server cannot be contacted (note default is 24 hours) before failing")
            # Abort the task for the following signals
            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) 
            if platform.system() != "Darwin":
                signal.signal(signal.SIGPWR,  self.signal_handler)
        def at_time(self):
    %ecfmicro #
            return datetime.datetime.fromtimestamp(time.time()).strftime('%H:%M:%S')
    #ecfmicro %
        def signal_handler(self,signum, frame):
            print('   Aborting: Signal handler called with signal ', signum)
  "Signal handler called with signal " + str(signum));
        def __enter__(self):
            print('Calling init at: ' + self.at_time())
        def __exit__(self,ex_type,value,tb):
            print ("   Client:__exit__: ex_type:" + str(ex_type) + " value:" + str(value) + "\n   traceback:" + str(tb))
            if ex_type is not None:
                print('Calling abort ' + self.at_time())
      "Aborted with exception type " + str(ex_type) + ":" + str(value))
            print('Calling complete at: ' + self.at_time())
  •  task wrapper:

    $include <>
    # This will also handle call to sys.exit(), i.e. Client.__exit__ will still be called.
    with Client() as ci:
       # *******************************************************************************
       # This is where the main work is done. 
       # *******************************************************************************
       for i in range(1,100):
          ci.child_label('info', "value_" + str(i))
       print "Finished event,meter and label child commands"
       This is the manual section. Instead of calling python from the ECF_JOB_CMD we could alternatively place,
       on the first line of this file.
       Note: We do not need a include a, the does it all.