[erlang-questions] port gotchas? (pegged CPU and sent machine to swap)

Howard Yeh <>
Fri Dec 26 03:53:25 CET 2008


Hi,

I am learning how to use port. What I am trying to do (for now) is to
spawn a script that reads from stdin and echoes back to stdout.

I read here [http://www.kazmier.com/computer/port-howto] that the echo
script should flush stdout, otherwise the spawning erlang process
would never hear back. But since I intend to use the script
asynchronously, I thought I'd just wait for the script's runtime
(ruby) to decide when it wants to flush.

So by my design, i should be able to:

(1) fire off requests to the subprocess asynchronously
(2) get results back as messages when the script decides to flush its IO.

This is the script,

while true
  if input = $stdin.readline
    i = Integer(input)
    $stdout.puts i+1
  end
end


But for some reason, after port_command is called, and waiting for response,

receive
        % for now, just assume we can get the whole line
        {Port,{data,{eol,Result}}} ->
            io:format("response: ~p~n",[Result]),
            Result;
 ...
end


This process goes to sleep, something else wakes up, pegs the CPU, and
sends my machine to swap. Presumably the port?

If I be a good boy and flush after each write to the pipe, it works fine,

while true
  if input = $stdin.readline
    i = Integer(input)
    $stdout.puts i+1
    $stdout.flush
  end
end


I am curious why that is...  Is there a port tracer to see what it's doing?

please help?

Howard



More information about the erlang-questions mailing list