[erlang-questions] io:format problem within a process, but not in the shell

John Hughes <>
Wed Dec 17 00:01:28 CET 2008


> From: Eric Holbrook <>
> Subject: > To: 
> Message-ID: <>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
> Why doesn't *all* of my io:format code work w/in the context of my
> process?
>
>
> <code>
>
> setMgr( Set ) ->
>    receive
>        {repr} ->
>            io:format("Set elements: ~n"),
>            [io:format("    ~p~n", [Elem]) || Elem <- sets:to_list(Set)],
>            setMgr(Set);
>        {other various blocks to add/modify my internal set 'Set'} ->
>            ...
>    end.
>
> </code>
>
> When i send the {repr} msg to a 'setMgr' process, i get the 'Set
> elements' output, but somehow the list comprehension part doesn't show
> up in STDOUT.
>
> However, if i create a set named Set in the shell, and execute the
> list comprehension line, it works fine.
>

Most likely reasons:

* the set is actually empty, so sets:to_list(Set) is the empty list [], and 
the list comprehension does nothing, or

* the set is not a set, and the sets:to_list(Set) raises a {badrecord,sets} 
exception, crashing the setMgr process. If you started the process with 
spawn_link, then you'll see the crash (the spawning process will receive an 
exit signal, and if it's the shell, print an error message). But if you 
started the process with spawn, then it will silently die at this point, 
giving the impression that your list comprehension did nothing.

Compare:

13> spawn_link(fun()->exit({badrecord,sets}) end).
** exception exit: {badrecord,sets}
14> spawn(fun()->exit({badrecord,sets}) end).
<0.59.0>

Check to see whether your process is still alive after the comprehension.
- use is_process_alive(Pid) on its pid, or
- send it another {repr} message and see if it prints "Set elements:" again, 
or
- add another io:format AFTER the list comprehension, and see if it is 
executed.

Hope that helps!

John 




More information about the erlang-questions mailing list