File Exchange

image thumbnail

Process: manage external processes asynchronously

version 1.0.0.0 (11.7 KB) by Emmanuel Farhi
This is a class to control external processes: a new command or connect to an already running one.

3 Downloads

Updated 21 Nov 2017

View License

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')) )

methods:
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:
findall(Process)

WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
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=get(pid,'UserData');
ud.UserData = <your stuff>;
set(pid, 'UserData', ud);
get your own data with:
ud=get(pid,'UserData');
<your stuff> = ud.UserData;
WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING

Example:
pid=Process('ping 127.0.0.1'); silent(pid);
pause(5);
exit(pid);

Copyright: Licensed under the BSD
E. Farhi, ILL, France <farhi@ill.fr> Aug 2012, http://ifit.mccode.org

Comments and Ratings (0)

MATLAB Release Compatibility
Created with R2010a
Compatible with any release
Platform Compatibility
Windows macOS Linux