How to use the spawn functions?

I thought the Elixir manual was great … until …

Typically developers do not use the spawn functions, instead they use abstractions such as …

So where do non-typical developers go to find out how to use functions that are in the manual?

I’m using the spawn functions so I can get my head around the agent / message sending thing, and understand how it works before I start using the abstractions.

I’ve already written code that uses spawn_monitor on one node … but now I’m trying to play with using another “remote” node. I was assuming there would be an equivalent spawn_monitor in Node, but apparently not. So now I’m looking at how to hand spawn_link … but there is nothing in the manual.

PS… I got my instruction on how to use the spawn_Monitor from “Programming Elixir” book … there’s no way I could have grok-ed the spawn functions without that external resource (so thanks Dave Thomas!).

Jonathan.

The Erlang Manual. You can call anything in Erlang from Elixir so the same manual works, just fix-up the syntax is all. :slight_smile:

In general, Elixir tries not to duplicate functionality from Erlang as you can still call Erlang.

1 Like

Since you’ve already got Programming Elixir you may also want to look at Elixir In Action (Manning) which I thought was particularly good in this area :023:

Well that’s depressing.

I’m sure the Erlang manual is fantastic if you already know Erlang … but I’ve always found it just a little bit more confusing that trying to read ancient Etruscan.

Have it … but haven’t started reading … was going to work through Programming first.

Of course I’m rushing ahead in my learning project … :slight_smile:

1 Like

I’m the opposite :101: I don’t like to start on a project until I’ve read enough to feel comfortable that I’ve got a good idea of what’s possible :slight_smile:

I’m currently reading Learn Functional Programming with Elixir (Pragprog) and I think will move on to Functional Web Development with Elixir, OTP, and Phoenix (Pragprog) next, unless PragDave’s next course comes out - in which case I’m inclined to drop everything and jump straight into that because I loved the first one so much :lol:

Whatever path you decide I’m sure you’ll enjoy your Elixir journey - good luck and keep us posted :023:

1 Like

@Astonj - yeah opposite … I’m learn by mistakes. Do it, get it wrong(maybe) and work out why. I find I come out with a better understanding.

So I found

Process.flag(:trap_exit, true)

that makes the spawning process get back an :EXIT message, like a spawn_monitor gets a :DOWN message.

Right. Now it runs remotely … but, it runs slower. Learning moment.

And now I think I have learnt that you have to really think about where the messages are going. What works in a single node doesn’t automatically work with a cluster of nodes.

I think the problem is that the remotely spawned processes are sending the results it generates (there are lots of them) back to the spawning process so it can evaluate and find the best. That ends up being slower that doing everything on the one node.

I have to now restructure the program so each process manages it’s own set of results, and pass the best back at the end, so I have one result per process.

Learning is fun … mostly. :slight_smile:

Jonathan

2 Likes

The Erlang syntax might be classic but it is actually much simpler and more consistent than Elixir however you look at it. Of course it very much depends on where you come from.

5 Likes

Could you expand on this or link something that explains this topic further? I would be interested to learn more about how the Elixir syntax is inconsistent :slight_smile:

1 Like

Solved? Really? It isn’t for me …

Please tell me where the any of the Elixir or Erlang manuals make any reference to what happens in the process that calls any of the spawn_link functions, apart from the spawn_link call return values …

My claim remains, you have to know how it works for the docs to actually be useful.

The tell you that those processes are linked. That’s it. You can not implement this link or a monitor on your own. It’s a feature of the BEAM. Asking for a way to implement it on your own is like asking how to reimplement MOV or STO in assembler.

1 Like

The documentation for spawn_link states:

For more information on linking, check Process.link/1.

The documentation for Process.link/1, where semantics of links are described. Does that give you enough information?

4 Likes

Yep, network IO is a real-life speed concern. ^.^

+++1

Oooo this is a big topic that has been talked about elsewhere and is suitable for another thread… ^.^;

|>

^.^