|
| 1 | +.. _execute: |
| 2 | + |
| 3 | +Execution of Tasks as Programs |
| 4 | +============================== |
| 5 | + |
| 6 | +Introduction |
| 7 | +------------- |
| 8 | + |
| 9 | +The :py:mod:`simpleflow.execute` module allows to define functions that will be |
| 10 | +executed as a program. |
| 11 | + |
| 12 | +There are two modes: |
| 13 | + |
| 14 | +- Convert the definition of a fonction as a command line. |
| 15 | +- Execute a Python function in another process. |
| 16 | + |
| 17 | +Please refer to the :py:mod:`simpleflow.tests.test_activity` test module for |
| 18 | +further examples. |
| 19 | + |
| 20 | +Executing a function as a command line |
| 21 | +-------------------------------------- |
| 22 | + |
| 23 | +Let's take the example of ``ls``: |
| 24 | + |
| 25 | +.. code:: |
| 26 | +
|
| 27 | + @execute.program() |
| 28 | + def ls(): |
| 29 | + pass |
| 30 | +
|
| 31 | +Calling ``ls()`` in Python will execute the ``ls`` command. Here the purpose of |
| 32 | +the function definition is only to describe the command line. The reason for |
| 33 | +this is to map a call in a workflow definition to a program to execute on the |
| 34 | +command line. The program may be written in any language whereas the workflow |
| 35 | +definition is in Python. |
| 36 | + |
| 37 | +Executing a Python function in another process |
| 38 | +---------------------------------------------- |
| 39 | + |
| 40 | +The rationale for this feature is to execute a function with another |
| 41 | +interpreter (such as pypy) or in another environment (virtualenv). |
| 42 | + |
| 43 | +.. code:: |
| 44 | +
|
| 45 | + @execute.python(interpreter='pypy') |
| 46 | + def inc(xs): |
| 47 | + return [x + 1 for x in xs] |
| 48 | +
|
| 49 | +Calling ``inc(range(10))`` in Python will execute the function with the |
| 50 | +``pypy`` interpreter found in the ``$PATH``. |
| 51 | + |
| 52 | + |
| 53 | +Limitations |
| 54 | +----------- |
| 55 | + |
| 56 | +The main limitation comes from the need to serialize the arguments and the |
| 57 | +return values to pass them as strings. Hence all arguments and return values |
| 58 | +must be convertible into JSON values. |
0 commit comments