MattyB
MattyB

Reputation: 183

Erlang ETS table events

Is there any established mechanism for waiting on an insert into an ETS table if I want to use it for shared state?

I know mnesia support table events, I was thinking a similiar mechanism would be useful for plain ETS so that a process could wait until the data it needed was loaded. I think the only way to do this is to spin loop and continuously request the given key until it arrives, but that seems very inefficient. I would rather get a message callback. If I insert a process between mine and ETS I've just pushed the problem down the line.

Upvotes: 3

Views: 419

Answers (3)

Hynek -Pichi- Vychodil
Hynek -Pichi- Vychodil

Reputation: 26131

There is not event handling in ets. It is pretty low-level language feature and is up to you to make a wrapper around it. BTW, It is way it is done in mnesia. So you can wrap your shared state into a module and then make subscribe and send yourself.

Upvotes: 1

Greg
Greg

Reputation: 8340

The simplest workaround would be to start a gen_server process and proxy all insert requests through it. You can then register callbacks by keeping them in the gen_server state and call them when the inserted key matches the pattern. It could be even running in a separate application to allow for a dedicated supervisor.

Upvotes: 3

Max Kuzmins
Max Kuzmins

Reputation: 9

The ETS doesn't have a concept of events.

However, you can achieve what you've described by putting a reader process into a 'receive' clause, then make a writer process send a message to the reader after data was loaded.

Upvotes: 1

Related Questions