What are messages in acting programming?

This question describes which actors play in acting programming. What are messages? How can you avoid the general state if you send an object in a message (assuming that objects exist in the programming of the actor)?

+3
source share
1 answer

If we think that the actors are people, then the messages are like ... messages.

Say that the boss wants the square root of the list of numbers a, and does not want to do all the calculations on his own. He can hire some Workers, and the Boss will find out their Phone Numbers.

, SMS : " a_i, 555-1234 , ". . .

 +------+  sqrt(i=0, a_i=9)         +------------+
 | Boss | ------------------------> | Worker 0   |
 +------+                           +------------+
       |   sqrt(i=1, a_i=16)        +------------+
       β€˜--------------------------> | Worker 1   |
                                    +------------+
                                       ....

, , SMS . .

 +------+   set_result(i=0, val=3)  +------------+
 | Boss | <------------------------ | Worker 0   |
 +------+                           +------------+
       ^  set_result(i=1, val=4)    +------------+
       β€˜--------------------------- | Worker 1   |
                                    +------------+
                                       ....

- , , . ( , .)

actor Boss:
   receive('run'):
     worker_addrs = spawn_many(SqrtWorker, len(a))  # hire workers.
     for i, addr in enumerate(worker_addrs): 
        send(addr, 'sqrt', reply_addr=self, i=i, a_i=a[i])

   receive('set_value', i, val):
     a[i] = val

actor SqrtWorker:
   receive('sqrt', reply_addr, i, a_i):
     send(reply_addr, 'set_value', i, sqrt(a_i))
     quit()

" ", . a . , a - .

, a shared? , .

 +------+  sqrt(i=0, a_phoneNum=555-1111)  +----------+
 | Boss | -------------------------------> | Worker 0 |
 +------+                                  +----------+

             +---+
             | a |
             +---+

( , a.)

 +------+                                 +----------+
 | Boss |                                 | Worker 0 |
 +------+                                 +----------+
                                               |
             +---+                             |
             | a | <---------------------------’
             +---+             get(i=0)

...

 +------+                                 +----------+
 | Boss |                                 | Worker 0 |
 +------+                                 +----------+
                                               ^
             +---+       list_val(i=0, val=9)  |
             | a | ----------------------------’
             +---+

list_val.

 +------+     set_result(i=0, val=3)      +----------+
 | Boss | <------------------------------ | Worker 0 |
 +------+                                 +----------+

             +---+
             | a |
             +---+

, ,

 +------+                                 +----------+
 | Boss |                                 | Worker 0 |
 +------+                                 +----------+
    | set(i=0, val=3)                
    |        +---+ 
    β€˜------> | a | 
             +---+ 

? - , a, , / . , .

+9

Source: https://habr.com/ru/post/1749544/


All Articles