Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>So, if I've understand you correctly - you don't want to return control from function <code>spawn_workers</code> until each of worker is not stopped (and fetched a page)? If that - you may change your code in such way:</p> <pre><code>spawn_workers(Ids) -&gt; %% collect reference to each worker Refs = [ do_spawn(Id) || Id &lt;- Ids ], %% wait for response from each worker wait_workers(Refs). wait_workers(Refs) -&gt; lists:foreach(fun receive_by_ref/1, Refs). receive_by_ref(Ref) -&gt; %% receive message only from worker with specific reference receive {Ref, done} -&gt; done end. do_spawn(Id) -&gt; Ref = make_ref(), proc_lib:spawn_link(?MODULE, do_send_req, [Id, {self(), Ref}]), Ref. do_send_req(Id, {Pid, Ref}) -&gt; io:format("Requesting ID ~p ... ~n", [Id]), Result = (catch ibrowse:send_req(to_url(Id), [], get, [], [], 10000)), case Result of {ok, Status, _H, B} -&gt; io:format("OK -- ID: ~2..0w -- Status: ~p -- Content length: ~p~n", [Id, Status, length(B)]), %% send message that work is done Pid ! {Ref, done}; Err -&gt; io:format("ERROR -- ID: ~p -- Error: ~p~n", [Id, Err]), %% repeat request if there was error while fetching a page, do_send_req(Id, {Pid, Ref}) %% or - if you don't want to repeat request, put there: %% Pid ! {Ref, done} end. </code></pre> <p><strong>Edit:</strong></p> <p>I've noticed that your entry point (function <code>start</code>) returns control without waiting for all workers are end their tasks (because of you calling there <code>spawn</code>). If you want to wait there too - just do the similar trick:</p> <pre><code>start() -&gt; ibrowse:start(), Ref = make_ref(), proc_lib:spawn(?MODULE, send_reqs, [self(), Ref]), receive_by_ref(Ref). send_reqs(Pid, Ref) -&gt; spawn_workers(fetch_ids()), Pid ! {Ref, done}. </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload