<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="272600" data-post-id="272600">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="jhogberg" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/jhogberg/120/24176_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  jhogberg
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Erlang Core Team</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>That’s not what I suggested. The idea is to run <code>createScaledImage</code> etc <em>in the spawned process</em> and send the image back. Your current implementation still runs all that in the requesting process, so it has the same problems as your old one.</p>
<p>The following ought to run it in the <code>Agent</code> instead. I haven’t checked if the code works or even compiles, but it will hopefully help you understand the general idea.</p>
<p>It would also be a good idea avoid using <code>Agent</code> for this since you may want these processes to die after they haven’t had any requests for a reasonable amount of time, or else they will hang around with their loaded images forever.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">  def image(conn, params) do
    sopinstanceuid = params["sopinstanceuid"]
    {width, ""} = Integer.parse(params["width"])
    {height, ""} = Integer.parse(params["height"])
    pid = ImageCache.get(sopinstanceuid)
    if pid do
      IO.puts("====&gt; CACHED! &lt;====")
      IO.puts "Pid: #{inspect pid}"
      pngimage = Agent.get(pid,
                           fn dcmff -&gt;
                              Dcmtknif.createScaledImage(dcmff, width, height, 0, 0)
                              Dcmtknif.setMinMaxWindow(scaledImage)
                              Dcmtknif.getPNG(wlImage)
                           end)
      conn
      |&gt; put_resp_content_type("image/png")
      |&gt; send_resp(200, pngimage)
    else
      im = ApiController.image(sopinstanceuid)
      IO.puts(im.imagepath)
      if File.exists?(im.imagepath) do
        IO.puts("====&gt; NOT CACHED, LOADING FROM FILE &lt;====")
        {:ok, pid} = Agent.start(fn -&gt; Dcmtknif.load(im.imagepath) end)
        ImageCache.put(sopinstanceuid, pid)
        image(conn, params)
      else
        conn
        |&gt; send_resp(404, "File not found")
      end
    end
  end
</code></pre> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="272600" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/long-running-c-nif-segfaults/52602/22">Post #21</a>
	                </div>
	            </div>
              <div id="likers-container-272600" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="272600"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #21"></div>
  </section>
</div>
    <div class="postbit" id="272601" data-post-id="272601">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="leonardorame" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  leonardorame
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Thanks!!!, now the the segfault has dissapeared.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="272601" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/long-running-c-nif-segfaults/52602/23">Post #22</a>
	                </div>
	            </div>
              <div id="likers-container-272601" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="272601"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-last-post cat-last-post" title="Last post!"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <span class="all-loaded">— All posts loaded —</span>
</div></template></turbo-stream>