Saturday, May 18, 2013

launch.py

Good news is - you don't have to write any extra code to run your Python application in a separate process (so called daemonization). All you need is - fully specified name of the method/function to execute. Something like:

    some_package.some_class.SomeClass.start_me
or
    some_package.some_script.main_function

Everything else is handled by launch.py [1].

launch.py is a set of friendly Python tools to install Virtual Environment, start, stop your application. In addition it gives you a single interface to test and analyze your code; provides configuration management.

In this post we will outline two features: installation and daemonization.

Installation. It's simple. launch.py will create a Virtual Environment for you and make sure that your application is executed within it. What you need to do is to download all of the required libraries for your application and place them in folder:
    launch.py/vendor 

Order of the libraries installation is defined by script:
    launch.py/scripts/install_virtualenv.sh


Once this step is complete just run ./launch.py -i to install Virtual Environment along with all of required libraries.

launch.py is also there to help with the application start and stop. There are two modes to run your application:
  • daemonized
    the application is started in a separate process
  • interactive
    the application is executed in the same command-line terminal where you have called launch.py and uses shared stdin, stdout and stderr to interact with the user
To use this feature simply follow the guide:
  1. Write the actual code
    List of the following assumptions is in place:
    - starter method or function has signature starter_name(*args)- classes implement __init__(self, process_name)
  2. Register your fully specified function/method name in launch.py/system/process_context.py as follows:
    PROCESS_CONTEXT = {
    ...
        'YOUR_PROCESS_NAME': _create_context_entry(
            process_name='YOUR_PROCESS_NAME',
            classname='workers.some_script.main',
            token='YOUR_PROCESS_NAME',
            time_qualifier=''),
    ...
    

    Should you wish to instantiate a class instance and start its method - simply define the class name:

            classname='workers.some_module.YourPythonClass.starter_name'
    

  3. ./launch.py --run --app YOUR_PROCESS_NAME will start your class' or script's starter method/function in a separate process (daemonized mode) 
  4. Add --interactive to the command above and run it in an interactive mode
In summary - launch.py is here to make your life easier. You can find details and read on many other features of the launch.py framework at its wiki [2].

Cheers!

[1] launch.py on the Github
https://github.com/mushkevych/launch.py

[2] launch.py WIKI
https://github.com/mushkevych/launch.py/wiki

No comments: