Content is available under Creative Commons Attribution.


Leroy Python Multi-Process Handling

Leroy has a built-in Python 3.x engine in it, and python code can actually be run in your workflow using <execute> steps like this:

Something to note when using Python in Leroy, is that the spacing is extremely sensitive. If you try to tab your python to match your XML tags, Python will give you an error; your python code must be left justified, and tabbed only as required by the language for proper nesting.

You can use the subprocess module in Python, which comes built in with Leroy, to ‘fork’ multiple processes in a cross-platform way.  Meaning, this should work for both Windows and Linux with the same code.  Let’s say, for instance, that you want to stop a group of Windows services in parallel.  In Leroy, you could do this:

What this code snip does, is define a list of windows service names in an array.  It iterates over that array, using subprocess.Popen to open each process independently (“in the background” so to speak).  It collects the process objects returned in a new list, called process.

Then, it iterates over all the process objects and waits for each to finish.  x.wait() actually returns the return codes if you want to collect them and include some logic as to what constitutes an error.  You could also redirect standard input / standard output / standard error if you wanted to process the output in Python.  If you don’t do it that way, the output will just be sent to Leroy and you can use a postProcess to check the output if desired.

This is, currently, the easiest cross-platform way to start a bunch of processes at the same time and let them run and a both powerful and simple use of the built-in Python engine.

Leave a Reply

by: Epic Force