Concurrency model based on message passing: the difference between Actor and CSP

An object oriented language is a language with good support for objects.
A concurrency oriented language has good support for concurrency.

--Joe Armstrong

Two general concurrency models: refer to the seven-week-seven concurrency model

  • Shared Memory Shared Memory
    • Threads
    • Locks
    • Mutexes
  • Message Passing (CSP and Actor models)
    • Process Processes
    • Messages
    • No shared data (status) No shared data

Focus on the comparison of the two models of message delivery Actor and CSP (Communicating Sequential Process)

Main purpose: In addition to the commonly used concurrency models for Python, Java, etc., there is such a thing

Look at two pieces of code first

Code sample comparison

Use Erlang code and Go code to implement the printing service print_server respectively, to compare the differences in model usage

Actor model - Erlang code

%%%-------------------------------------------------------------------
%%% @author Suncle
%%% @doc
%%% print_server
%%% @end
%%% Created : 2017/12/18 14:53
%%%-------------------------------------------------------------------
-module(print_server).
-author("Flowsnow").

%% API
-export([print_server/0, start_print_server/0, send_msg/2]).


print_server() ->
  receive
    Msg ->
      io:format("print_server received msg: ~p~n", [Msg]),
      print_server()
  end.

start_print_server() ->
  Pid = spawn(?MODULE, print_server, []),
  Pid.

send_msg(Msg, Pid) ->
  Pid ! Msg,
  io:format("send_normal_msg: ~p~n", [Msg]).

The output of the Erlang shell is as follows:

1> c("print_server.erl").
{ok,print_server}
2> Pid = print_server:start_print_server().
<0.39.0>
3> print_server:send_msg("hello", Pid).
send_normal_msg: "hello"
print_server received msg: "hello"
ok

The above print_server is implemented using the most primitive Erlang syntax, and it can also be realized more clearly and easily by using the OTP gen_server primitive

CSP model - Go code

The print function reads the message from the channel and blocks until the main function writes the hello message to the channel

package main

import (
    "fmt"
    "time"
)

func main() {
    c := make(chan string)
    go print(c)
    time.Sleep(1 * time.Second)
    fmt.Println("main function: start writing msg")
    c <- "hello"

    var input string
    fmt.Scanln(&input)
}

func print(c <-chan string) {
    fmt.Println("print function: start reading")
    fmt.Println("print function: reading: " + <-c)
    time.Sleep(1 * time.Second)
}

The output is as follows:

D:\workspace\Go>go run print_server.go
print function: start reading
main function: start writing msg
print function: reading: hello

Model diagram comparison

Actor

Actor1 sends a message to Actor2's mailbox, which is essentially a queue and consumed by Actor2

CSP

Process1 adds a message at the write end of the Channel, and Process2 reads the message at the read end of the channel

Basic feature comparison

Actor

  1. Based on message passing message-passing
  2. Message and mailbox mechanism: messages are sent asynchronously
  3. mutable state is preserved but not shared
  4. Failure detection and letting it crash
  5. The focus is on the entity when sending the message

CSP

  1. Based on message passing message-passing
  2. Sequential processes Sequential processes
  3. Synchronous communication through channels
  4. Multiplexing of channels with alternation
  5. The focus is on the channel channel used when sending messages

Comparison of Communication Semantics

Actor

Actor1 waits for a message and blocks until Actor2 sends a message to Actor1

Actor2 sends a message to Actor3 and temporarily stores it in Actor3's Mailbox until Actor3 accepts and processes it

CSP

Process1 reads the channel because there is no message blocking until Process2 adds a message to the channel

Process2 adds a message to the channel and blocks until Process3 reads the channel message

Erlang implements simple bank accounts

Using Erlang primitives, the code is as follows:

Using OTP's gen_server, the code is as follows:

Erlang Small Project: IP Database

Using the IP database implemented by Erlang/OTP, you can query the specific country and province according to the IP. The code is as follows:

Different Erlang features

  1. Let it crash thought: worth learning

For example: execute arithmetic exception crash

  1. Variables are immutable, and once a variable is assigned a value, it cannot be changed: the advantage is that there is no mutable state, no memory sharing, and no lock
  2. The only means of interaction between Erlang processes is message passing: Erlang does not have many different means of interaction between processes (pipes, message queues, memory sharing, etc.) like C++.

FAQ

Why is there no auto-increasing buffer?

Even if there is a seemingly inexhaustible resource now, it will eventually be exhausted. It may be due to the passage of time, and the old program now needs to solve larger-scale problems; it may also be that there is a bug, and the messages are not processed in time, resulting in accumulation. If you don't think about the countermeasures when the buffer is full, then at some time in the future, there may be a very destructive, deeply hidden and difficult to diagnose bug. The best strategy is to think about how to deal with the buffer being full now and nip the problem in the bud.

Therefore, there are three commonly used buffer types: blocking type (blocking), discarding new value type (dropping), and removing old value type (sliding)

What message passing concurrency model does Python have?

Actor model pykka:https://github.com/jodal/pykka

CSP模型pycsp:https://github.com/runefriborg/pycsp/wiki/Getting_Started_With_PyCSP


The pictures are all from here !


reference:


Remember to give me a thumbs up!

Carefully sort out the video courses and e-books in all directions of computer, from introductory, advanced, and practical, and classify them reasonably according to the catalog. You can always find the learning materials you need. What are you waiting for? Follow and download now! ! !

resource-introduce

If you never forget, there will be echoes, friends, please give me a thumbs up, thank you very much.

I am a bright brother in the workplace, a YY senior software engineer with four years of work experience, and a slash programmer who refuses to be the leader.

Listen to me, a lot of progress, the life of the program is a shuttle

If you are lucky enough to be able to help you, please give me a [Like], give me a follow, and if you can comment to give encouragement, it will be very grateful.

Workplace Liangge article list: more articles

wechat-platform-guide-attention

All my articles and answers have cooperated with the copyright protection platform, and the copyright belongs to Brother Liang in the workplace. Unauthorized reprinting will be punished!

Guess you like

Origin blog.csdn.net/u013637931/article/details/111356480