apr
Child processes can modify ETS table?
Hello!
Is there any way to specify that an ets table can be modified only by the owning process and the children of the owning process? Setting protection to public works but allows any process to modify the table.
A usecase would be to spawn multiple children in the owning process (using task_async), each of will modify a unique set of rows in the ets table.
Thanks!
Most Liked
alco
A :public table may or may not also be :named. An unnamed table can only be accessed using the table reference returned from :ets.new. Pass that reference to your child processes and only they will be able to work with the table.
Here’s some code that demonstrates this:
iex(1)> t = :ets.new(:secret_table, [:public])
#Reference<0.2500864750.288227330.231076>
iex(2)> spawn(fn -> IO.inspect :ets.lookup(:secret_table, :foo) end)
#PID<0.105.0>
10:13:17.716 [error] Process #PID<0.105.0> raised an exception
** (ArgumentError) argument error
(stdlib) :ets.lookup(:secret_table, :foo)
(stdlib) erl_eval.erl:680: :erl_eval.do_apply/6
(stdlib) erl_eval.erl:888: :erl_eval.expr_list/6
(stdlib) erl_eval.erl:411: :erl_eval.expr/5
iex(4)> spawn(fn -> IO.inspect :ets.lookup(t, :foo) end)
#PID<0.108.0>
[]
tty
Unfortunately no. You can do some tricks using give_away but its really more trouble than its worth.
tty
If the spawning process inherits the table won’t that make it the only process which can write to the table forcing a serialization point ?
I think you just got yourself a nice load testing project ![]()
At this point I’m not convince you are getting much gains over:
- Serializing all writes through one GenServer or
- Just making
etstable public and allowing the child processes to write directly to it
You make (2) a bit more protected by not naming the table and passing the Tid down to the child processes.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









