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


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="Kallee" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  Kallee
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I have built an ETL in Python that I want to call from Elixir. I wanted to ask if you have any suggestions on how to best pass maps/dictionaries between Elixir and Python. Some of the maps/dicts will contain simple structures, but others will contain more complex structures, such as pandas DataFrames.</p>
<p>I have looked into both serialization via JSON and writing custom functions on each side. I assume this will be quite a common use case for Venomous, so I wanted to ask your opinion on this.</p>
<p>Thanks for a great library with good documentation; it’s been a great introduction to the world of Elixir!</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="337806" data-batch-url="/posts/batch_likers">
                        1
                      </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/venomous-erlport-wrapper-for-managing-concurrent-python-processes-with-ease/64134/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-337806" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="337806"
                     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 #11"></div>
  </section>
</div>
    <div class="postbit" id="337840" data-post-id="337840">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="RustySnek" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/RustySnek/120/34995_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  RustySnek
                    <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">
								<aside class="quote no-group" data-username="Kallee" data-post="12" data-topic="64134">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/k/8dc957/48.png" class="avatar"> Kallee:</div>
<blockquote>
<p>ETL</p>
</blockquote>
</aside>
<p>Hey, for simple classes that can be easily serialized with <code>.__dict__</code> you can just handle that recursively for basic data types. <code>venomous.py</code> provides a function that does handles such cases and encodes all strings into ‘utf-8’ so they won’t appear as charlists on elixir’s side.</p>
<pre data-code-wrap="python"><code class="lang-python">def encode_basic_type_strings(data: Any):
    """
    encodes str into utf-8 bytes
    handles VenomousTrait classes into structs
    converts non VenomousTrait classes into .__dict__
    """
    if isinstance(data, str):
        return data.encode("utf-8")
    elif isinstance(data, (list, tuple, set)):
        return type(data)(encode_basic_type_strings(item) for item in data)
    elif isinstance(data, dict):
        return {
            encode_basic_type_strings(key): encode_basic_type_strings(value)
            for key, value in data.items()
        }
    elif isinstance(data, VenomousTrait):
        return data.into_erl()

    elif (_dic := getattr(data, "__dict__", None)) != None:
        return encode_basic_type_strings(_dic)
    else:
        return data
</code></pre>
<p>If you want to maintain the structs/classes between elixir/python you can experiment with <a href="https://github.com/RustySnek/Venomous?tab=readme-ov-file#structclass-comp" rel="noopener nofollow ugc">VenomousTrait class</a> all tho I haven’t documented it very well yet.<br>
As for the more complex structures you have to handle them individually, like for example DataFrames provides <code>to_dict()</code> function which returns a clean dict with data. All of the logic of conversion should be put inside the <a href="https://github.com/RustySnek/Venomous/blob/master/PYTHON.md" rel="noopener nofollow ugc">encoder/decoder</a> functions of erlport. So for the DataFrame you could do:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">```python
# encoder.py
from typing import Any
from erlport.erlang import set_decoder, set_encoder
from erlport.erlterms import Atom
from pandas import DataFrame
from venomous import decode_basic_types_strings, encode_basic_type_strings


def handle_types():
    set_encoder(encoder)
    set_decoder(decoder)
    return Atom("ok".encode("utf-8"))


def encoder(value: Any):
    if isinstance(value, DataFrame):
        return encode_basic_type_strings(value.to_dict())
    return encode_basic_type_strings(value)


def decoder(value: Any):
    return decode_basic_types_strings(value)
</code></pre>
<pre data-code-wrap="python"><code class="lang-python"># data_frames.py
import pandas as pd

def data_frames(dict):
    df = pd.DataFrame(dict)
    return df
</code></pre>
<pre data-code-wrap="elixir"><code class="lang-elixir">iex(16)&gt; df = %{
...(16)&gt;   "Age" =&gt; %{0 =&gt; 25, 1 =&gt; 30, 2 =&gt; 35, 3 =&gt; 40},
...(16)&gt;   "City" =&gt; %{0 =&gt; "New York", 1 =&gt; "London", 2 =&gt; "Paris", 3 =&gt; "Tokyo"},
...(16)&gt;   "Name" =&gt; %{0 =&gt; "John", 1 =&gt; "Jane", 2 =&gt; "Bob", 3 =&gt; "Alice"}
...(16)&gt; }
%{
  "Age" =&gt; %{0 =&gt; 25, 1 =&gt; 30, 2 =&gt; 35, 3 =&gt; 40},
  "City" =&gt; %{0 =&gt; "New York", 1 =&gt; "London", 2 =&gt; "Paris", 3 =&gt; "Tokyo"},
  "Name" =&gt; %{0 =&gt; "John", 1 =&gt; "Jane", 2 =&gt; "Bob", 3 =&gt; "Alice"}
}
iex(17)&gt; Venomous.SnakeArgs.from_params(:data_frames, :data_frames, [df]) |&gt; Venomous.python() 
%{
  "Age" =&gt; %{0 =&gt; 25, 1 =&gt; 30, 2 =&gt; 35, 3 =&gt; 40},
  "City" =&gt; %{0 =&gt; "New York", 1 =&gt; "London", 2 =&gt; "Paris", 3 =&gt; "Tokyo"},
  "Name" =&gt; %{0 =&gt; "John", 1 =&gt; "Jane", 2 =&gt; "Bob", 3 =&gt; "Alice"}
}
</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="337840" data-batch-url="/posts/batch_likers">
                        2
                      </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/venomous-erlport-wrapper-for-managing-concurrent-python-processes-with-ease/64134/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-337840" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="337840"
                     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 #12"></div>
  </section>
</div>
    <div class="postbit" id="351024" data-post-id="351024">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="vtno" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/vtno/120/37065_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  vtno
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Awesome library!</p>
<p>I’m building a PoC on some machine learning API which uses Elixir to manage Python processes for NLP task.</p>
<p>There is a small cold start when the method is invoke the first time with Venoumous.python call. I wonder if there is an easy way to pre-start some worker so there is no cold start time when running the program?</p>
<p>I’m looking at the SnakeWorker/Supervisor but not sure if it’s the correct place.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="351024" data-batch-url="/posts/batch_likers">
                        2
                      </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/venomous-erlport-wrapper-for-managing-concurrent-python-processes-with-ease/64134/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-351024" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="351024"
                     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 #13"></div>
  </section>
</div>
    <div class="postbit" id="351130" data-post-id="351130">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="RustySnek" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/RustySnek/120/34995_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  RustySnek
                    <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>Hey, I’m happy you found the library helpful! ^^</p>
<p>I have added <code>Venomous.preload_snakes/1</code> in the 0.7.5 release, which basically starts x amount of processes with :ready state. So you can basically start workers at the start of your program with:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">:ok = Venomous.preload_snakes(10) # Starts 10 workers
{:retrieve_error, :max_children} = Venomous.preload_snakes(-1) # Starts all available workers
</code></pre>
<p>lmk if it helped!</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="351130" data-batch-url="/posts/batch_likers">
                        1
                      </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/venomous-erlport-wrapper-for-managing-concurrent-python-processes-with-ease/64134/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-351130" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="351130"
                     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 #14"></div>
  </section>
</div>
    <div class="postbit" id="351140" data-post-id="351140">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="vtno" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/vtno/120/37065_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  vtno
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>That should work! Another nice thing to add is to shutdown the workers during termination.</p>
<p>I noticed that you already have the terminate hook in the worker</p>
<p><a href="https://github.com/RustySnek/Venomous/blob/master/lib%2Fsnake_worker.ex#L133" class="onebox" target="_blank" rel="noopener nofollow ugc">https://github.com/RustySnek/Venomous/blob/master/lib%2Fsnake_worker.ex#L133</a></p>
<p>but not sure why when my supervisor exited by Application.stop. There are hanging erlport processes.</p>
<p>I need to add my own list_alive_snake and slay them manually on my terminate hook.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="351140" data-batch-url="/posts/batch_likers">
                        1
                      </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/venomous-erlport-wrapper-for-managing-concurrent-python-processes-with-ease/64134/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-351140" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="351140"
                     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 #15"></div>
  </section>
</div>
    <div class="postbit" id="351247" data-post-id="351247">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="RustySnek" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/RustySnek/120/34995_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  RustySnek
                    <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>Hey, I don’t encounter such problem when I do <code>Application.stop(:venomous)</code>. However you mentioned that you exit a different supervisor so perhaps you would have to link them so they terminate alongside each other? Calling stop on <code>:venomous</code> is also a way.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="351247" 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/venomous-erlport-wrapper-for-managing-concurrent-python-processes-with-ease/64134/17">Post #16</a>
	                </div>
	            </div>
              <div id="likers-container-351247" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="351247"
                     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 #16"></div>
  </section>
</div>
    <div class="postbit" id="351266" data-post-id="351266">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="vtno" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/vtno/120/37065_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  vtno
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Actually it took a while for the process to be removed. After I waited a bit, ps -aux | grep erlport does not show running worker anymore so all good!</p>
<p>I’ve been using the preload as well and it works perfectly. A little curious about the reason why you make the return value when using -1 as <code>{:retrieve_error, :max_children}</code> instead of <code>:ok</code> when it’s successful?</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="351266" 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/venomous-erlport-wrapper-for-managing-concurrent-python-processes-with-ease/64134/18">Post #17</a>
	                </div>
	            </div>
              <div id="likers-container-351266" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="351266"
                     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 #17"></div>
  </section>
</div>
    <div class="postbit" id="351280" data-post-id="351280">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="RustySnek" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/RustySnek/120/34995_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  RustySnek
                    <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>It wasn’t really well thought out as if you just supply the function with -1 it will keep on spawning workers until it encounters the error which in this case will be the :max_children. It’s kind of a way of signaling that you have reached the limit. I might change it later on to make a little bit more sense as it’s not really an <code>error</code> if everything did work as intended.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="351280" 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/venomous-erlport-wrapper-for-managing-concurrent-python-processes-with-ease/64134/19">Post #18</a>
	                </div>
	            </div>
              <div id="likers-container-351280" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="351280"
                     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>