I want to start a bunch of subprocesses and write some lines to each of them
with some configuration data, then write filenames to them to process, and then
tell them there's no more work to do. Then I want to gather any results they
produce. The key function is this:
proc doJobs(config: Config) =
var processes = newSeq[Process](countProcessors())
# Initialize all the processes
for i in 0..processes.high:
var process = startProcess(config.subcommand,
options={poStdErrToStdOut, poDemon})
var stream = process.inputStream()
stream.writeLine("*regex=", config.regex) # BAD
stream.writeLine("*configured")
# Dish out the work to all the processes: TODO do it "fairly"
var i = 0
for filename in getFilenames(config):
var process = processes[i mod processes.len()]
var stream = process.inputStream()
stream.writeLine(filename)
# Tell them there's no more work to do
for i in 0..processes.high:
processes[i].inputStream.writeLine("*finished")
# Handle any results they produce
while processes.len() > 0:
var i = random(processes.len())
var process = processes[i]
if not process.running():
process.close()
processes.delete(i)
else:
for line in lines(process.outputStream()):
echo line
Unfortunately this won't compile. The problem is at the line marked BAD which
produces the error message:
searchcmd.nim(41, 11) Error: type mismatch: got (Stream, string, string)
but expected one of:
proc writeLine[Ty](f: File; x: varargs[Ty, `$`])
I also tried using inputHandle() instead of inputStream() but the only
difference that made was in the type mismatch: got (FileHandle, string, string).
Can anyone advise?