On Sun, Mar 26, 2017 at 11:24 PM, huliuhe <[email protected]> wrote:

> I create a button node : @button test . and write the python code:
>

​Use Python's subprocess module
<https://docs.python.org/2/library/subprocess.html>​ if you are going to
run just one process and wait for the result. For example:

    proc = subprocess.Popen(
            command,
            stdout=subprocess.PIPE,
            shell=False,
            universal_newlines=True, # Converts stdout to unicode
        )
    # wait
    stdout_data, stderr_data = proc.communicate()

To run multiple processes without blocking Leo, use
g.app.backgroundProcessManager, a singleton instance of the
BackgroundProcessManager class in leoBackground.py.

This @file node was omitted by accident from leoPyRef.leo in Leo 5.5 final.
I restored it several days ago on Leo's git repository. To restore this
node by hand, create it in your leoPy.leo and use refresh-from-disk.

Here is the docstring from that class:

The BackgroundProcessManager (BPM) class runs background processes,
*without blocking Leo*. The BPM manages a queue of processes, and runs them
*one at a time* so that their output remains separate.

g.app.backgroundProcessManager is the singleton BPM.

The BPM registers a handler with the IdleTimeManager that checks whether
the presently running background process has completed. If so, the handler
writes the process's output to the log and starts another background
process in the queue.

BPM.start_process(c, command, kind, fn=None, shell=False) adds a process to
the queue that will run the given command.

BM.kill(kind=None) kills all process with the given kind. If kind is None
or 'all', all processes are killed.

You can add processes to the queue at any time. For example, you can rerun
the 'pylint' command while a background process is running.

The BackgroundProcessManager is completely safe: all of its code runs in
the main process.

**Running multiple processes simultaneously**

Only one process at a time should be producing output. All processes that
*do* produce output should be managed by the singleton BPM instance.

To run processes that *don't* produce output, just call subprocess.Popen.
You can run as many of these process as you like, without involving the BPM
in any way.

*Example* The node:

leoPy.leo#Code-->Command classes-->@file
../commands/checkerCommands.py-->class PylintCommand-->pylint.run_pylint

contains the code that runs pylint.  self.wait is False, so after setting
up the desired pylint command the code just executes:

    bpm = g.app.backgroundProcessManager
    bpm.start_process(c, command, kind='pylint', fn=fn)

Setting up the command can be a bit tricky.  Note that you may have to use
shlex, depending on platform.

HTH.

Edward

-- 
You received this message because you are subscribed to the Google Groups 
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/leo-editor.
For more options, visit https://groups.google.com/d/optout.

Reply via email to