As Mr.Wizard pointed out, you can do this with ParallelSubmit
. But you need a little more, and that's the (not so) tricky part as it is not very well documented. I think something like the following should work for you:
Needs["Parallel`Developer`"]
f[x_] := (Pause[x]; x)
LaunchKernels[1]
DistributeDefinitions[f]
eid = ParallelSubmit[f@5]
QueueRun[]
Now you can go do some other work. When you think the external program has finished you do
WaitNext@{eid}
to collect the result.
Let me briefly explain what happens in the above code: First, I launch as many slave kernels as external functions I want to run in parallel (in this case only 1). Then the definition of f
is distributed to all slave kernels. The code will also work without this step, you will not gain any speedup, however. (If the definition of f
is not distributed, the slave kernels just return f@5
to the master, who will evaluate it and therefore pause for 5 seconds.) ParallelSubmit
submits the computation to the queue. At this point the slave kernels are still idling. The crucial step is to run QueueRun
, which is loaded from the Developer
context. It starts the parallel queue but returns immediately, without waiting for results from the slave kernels. Results are collected by WaitNext
(or WaitAll
if you want), but as the name says, this does not return until it gets the next result from the slaves, so you can't run other computations at the same time.
To address your problem a bit more precisely, the following worked for me (on Linux that is):
Needs["Parallel`Developer`"]
LaunchKernels[2]
cmd := "echo " <> ToString[$KernelID] <> " >> foo.txt"
DistributeDefinitions[cmd]
eid = {ParallelSubmit@Run[cmd], ParallelSubmit@Run[cmd]}
QueueRun[]
WaitAll[eid]
As I understand it, the functionality in the Parallel`Developer context evolved from the "Parallel Computing Toolkit" by Roman Maeder, which was at some point integrated into main MMA. Unfortunately the documentation largely got lost in the process (which is really a shame!), but it is still available at Wolfram. The example section is definitely worth a read! Note that some commands where renamed though.
ParallelSubmit
. $\endgroup$