Process: manage external processes asynchronously

versión (11.7 KB) por Emmanuel Farhi
This is a class to control external processes: a new command or connect to an already running one.

81 descargas

Actualizada 21 Nov 2017

Ver licencia

Process(command): starts a system command

pid = Process('command arguments ...')

The Process class replaces the 'system' command. but is started asynchronously.
Matlab does not wait for the end of the Process to get back to interactive mode.
The stdout and stderr are collected periodically.

You can as well monitor an existing external process by connecting to its PID (number)

pid = process(1234);
pid = connect(Process, 1234);

or by connecting to a running named process:

pid = connect(Process, 'ping');

You can customize the Process with e.g. additional arguments such as:
Process(..., 'TimeOut', value) set a TimeOut (to kill Process after)
Process(..., 'Period', value) set the refresh rate in seconds (10 s).
Process(..., 'Monitor', 0 or 1) flag to set the Process in silent/verbose mode
Process(..., 'TimerFcn', @fcn) execute periodically on refresh
Process(..., 'StopFcn', @fcn) execute when the Process is killed (stop/exit)
Process(..., 'EndFcn', @fcn) execute when the Process ends by itself

The TimerFcn, StopFcn and EndFcn can be given as:
* simple strings, such as 'disp(''OK'')'
* a function handle with none to 2 arguments. The Callback will then
pass as 1st argument the Process object, and as 2nd the event
in 'kill','timeout','end', or 'refresh'.
Example @(p,e)disp([ 'Process ' p.Name ': event ' e ])
* the name of a function which takes none to 2 arguments. Same as above.
when a callback has a non-zero return value, it stops the Process.

For instance:
to stop a Process when a file appears, use:
Process(..., 'TimerFcn', @(p,e)~isempty(dir('/path/file')) )
to stop a Process when a file disappears, use:
Process(..., 'TimerFcn', @(p,e)isempty(dir('/path/file')) )

refresh(pid) force the pid to be refreshed, i.e check if it is running
and get its stdout/stderr.
silent(pid) set the process to silent mode (do not print stdout/stderr).
verbose(pid) set the process to verbose mode (print stdout/stderr).
disp(pid) display full Process information.
pid display short Process information. Same as display(pid).
stdout(pid) get the stdout stream from the Process (normal output).
stderr(pid) get the stderr stream from the Process (errors).
exit(pid) kill the Process (stop it). Same as stop(pid)
delete(pid) kill the Process and delete it from memory.
waitfor(pid) wait for the Process to end normally or on TimeOut.
isreal(pid) check if the process is valid/running.
killall(Process) kill all running Process objects

To get all running Process objects, use:

This class derives from 'timer'. As such, it remains in memory after the
Process has stopped. To remove it, use delete(pid), then clear(pid).

The UserData field of the object is used by the external command. In case
you need to store something, do NOT directly allocate pid.UserData, but
rather store anything with set and get calls:
ud.UserData = <your stuff>;
set(pid, 'UserData', ud);
get your own data with:
<your stuff> = ud.UserData;

pid=Process('ping'); silent(pid);

Copyright: Licensed under the BSD
E. Farhi, ILL, France <> Aug 2012,

Citar como

Emmanuel Farhi (2022). Process: manage external processes asynchronously (, MATLAB Central File Exchange. Recuperado .

Compatibilidad con la versión de MATLAB
Se creó con R2010a
Compatible con cualquier versión
Compatibilidad con las plataformas
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!