This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision Next revision Both sides next revision | ||
cs326:lab5 [2021/10/24 23:03] scarl |
cs326:lab5 [2021/10/24 23:11] scarl |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | === Lab 5 === | + | ==== CSci 326 Lab #5 - Sending Messages ==== |
We're going to be following the discussion and code experiments in this blog post by Piotr Szymański: | We're going to be following the discussion and code experiments in this blog post by Piotr Szymański: | ||
Line 10: | Line 10: | ||
* To start, use the module name ''Table'' as in the blog. When you get to the section ''Sending messages back and forth between processes'' put that code in a new module called ''Table2'' and use that name in the tests that follow. | * To start, use the module name ''Table'' as in the blog. When you get to the section ''Sending messages back and forth between processes'' put that code in a new module called ''Table2'' and use that name in the tests that follow. | ||
* In the section after we start the Observer using '':observer.start'' Piotr suggests running 100 million processes. **Don't.** If you do too many sends, the process mailbox can get *very* full when the process can't keep up - my poor 4GB MacBook died. Rather, start small and build up to 1 million (this will //still// take some time). For example:<code>p = Table.start | * In the section after we start the Observer using '':observer.start'' Piotr suggests running 100 million processes. **Don't.** If you do too many sends, the process mailbox can get *very* full when the process can't keep up - my poor 4GB MacBook died. Rather, start small and build up to 1 million (this will //still// take some time). For example:<code>p = Table.start | ||
- | 1..100 |> Stream.map(fn _ -> send p, :ping end) |> Enum.count</code> | + | 1..100 |> Stream.map(fn _ -> send p, :ping end) |> Enum.count |
+ | 1..1000 |> Stream.map(fn _ -> send p, :ping end) |> Enum.count | ||
+ | #...and so on...</code> | ||
* If you want to try stress-testing with 100 million processes, do it **last**...and on the Linux box, not a laptop. | * If you want to try stress-testing with 100 million processes, do it **last**...and on the Linux box, not a laptop. | ||
| | ||
- | + | For full credit, show me your **iex** session after you complet the last section, working with ''Table2''. | |
- | <code> | + | |
- | # Table - lessons on using processes and message-passing from http://eddwardo.github.io/ | + | |
- | # | + | |
- | # version one: spawn a process running a tail-recursive fcn that does nothing but receive :ping messages | + | |
- | # | + | |
- | + | ||
- | defmodule Table do | + | |
- | def ping do | + | |
- | receive do | + | |
- | :ping -> IO.puts('received ping') | + | |
- | end | + | |
- | ping() | + | |
- | end | + | |
- | + | ||
- | def start do | + | |
- | IO.puts __MODULE__ | + | |
- | # as shown by the IO.puts, __MODULE__ evaluates to the (fully-qualified) module name | + | |
- | spawn(__MODULE__, :ping, []) | + | |
- | end | + | |
- | end | + | |
- | </code> | + | |
- | + |