Run programs in the background from Mathematica
Is there any way to make a background process in Mathematica so that the external command will run but not hold up other evaluations?
startTime = A开发者_运维知识库bsoluteTime[];
Run["sleep 2" (*, a magic option here perhaps? *)];
AbsoluteTime[] - startTime
(* Returns 2.016960 on my system *)
I want a version of / option for Run
that will not hold up the evaluation, so that the last line in the above code will return something close to zero.
(The above will run in *nix and OSX; there doesn't seem to be any universal/portable way to "sleep 2" in a DOS/Windows shell:
Sleeping in a batch file. Although, perhaps the easiest way to get the sleep
command is to install the MS resource kit - see this answer to the Sleeping in a batch file question or the comments below.)
At the moment, the reason I want this is so that my ALSASound
command given in No sound in Mathematica under linux will not stop the fireworks. But it would also be a good improvement to ALSASound
and a useful thing to know in general.
Edit
The two solutions posted below rely on using the shell to spawn a new/child process using start cmd
in Windows/DOS and cmd&
in *nix. Is there a platform independent and/or Mathematica based solution? (Points will be awarded for the creation of a command that checks the OS using $OperatingSystem
!)
As Simon points out, Switch
is the correct tool for procedural conditions and is more appropriate than Piecewise
. Here's the same solution using Switch
:
BackgroundProcess[proc_String] :=
Switch[$OperatingSystem,
"MacOSX" | "Unix", Run["(" <> proc <> ")&"],
"Windows", Run["start /b " <> proc],
_, $Failed]
You could write a wrapper function like so that handles multiple OSes.
BackgroundProcess[proc_String] :=
Module[{
command = Piecewise[{
{proc <> " &", $OperatingSystem == "MacOSX" || "Unix"},
{"start /b " <> proc, $OperatingSystem == "Windows"}
}]
},
Run[command]]
The /b
after start
runs the process in the background, without spawning a new command window and the output is printed to stdout. You can then build upon this to include error messages, some input sanitization (e.g., don't allow rm
and the like) if you want that, and you should be all set.
A predefined function from Mathematica (if it existed) is probably going to be along these lines. Background processes are inherently OS dependent, so there is no real "platform independent" way of doing it. Any implementation that claims to be so (like the os module in python) is basically a set of rules defined for each possible OS known to exist, so that you don't really have to worry about the finer details.
In Windows (Did I understand what you are trying to do ??)
startTime = AbsoluteTime[];
Run["start notepad"];
AbsoluteTime[] - startTime
(*
-> 0.1406250
*)
Edit
For portability reasons, I don't think Mma should assume particular features of the underlying OS. Having a Standard Input and Output, and a Command shell callable from inside a program is enough. Once you get control passed to the OS shell of your choice, do whatever you want (call start
or submit &
, for example) or what your OS allows you to do.
Although I don't know a Mathematica way of doing this, there is a very simple BASH/shell solution: just create a a child process using "&
".
Then
startTime = AbsoluteTime[];
Run["(sleep 20)&"];
AbsoluteTime[] - startTime
(* Returns approx 0.01 *)
A similar solution works with my ALSASound
command and I've updated my answer.
精彩评论