/
Actor.html
1302 lines (884 loc) · 90.2 KB
/
Actor.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>
Module: Concurrent::Actor
— Concurrent Ruby
</title>
<link rel="stylesheet" href="../css/style.css" type="text/css" charset="utf-8" />
<link rel="stylesheet" href="../css/common.css" type="text/css" charset="utf-8" />
<script type="text/javascript" charset="utf-8">
pathId = "Concurrent::Actor";
relpath = '../';
</script>
<script type="text/javascript" charset="utf-8" src="../js/jquery.js"></script>
<script type="text/javascript" charset="utf-8" src="../js/app.js"></script>
</head>
<body>
<div class="nav_wrap">
<iframe id="nav" src="../class_list.html?1"></iframe>
<div id="resizer"></div>
</div>
<div id="main" tabindex="-1">
<div id="header">
<div id="menu">
<a href="../_index.html">Index (A)</a> »
<span class='title'><span class='object_link'><a href="../Concurrent.html" title="Concurrent (module)">Concurrent</a></span></span>
»
<span class="title">Actor</span>
</div>
<div id="search">
<a class="full_list_link" id="class_list_link"
href="../class_list.html">
<svg width="24" height="24">
<rect x="0" y="4" width="24" height="4" rx="1" ry="1"></rect>
<rect x="0" y="12" width="24" height="4" rx="1" ry="1"></rect>
<rect x="0" y="20" width="24" height="4" rx="1" ry="1"></rect>
</svg>
</a>
</div>
<div class="clear"></div>
</div>
<div id="content"><h1>Module: Concurrent::Actor
</h1>
<div class="box_info">
<dl>
<dt>Defined in:</dt>
<dd>lib-edge/concurrent/actor.rb<span class="defines">,<br />
lib-edge/concurrent/actor/core.rb,<br /> lib-edge/concurrent/actor/root.rb,<br /> lib-edge/concurrent/actor/utils.rb,<br /> lib-edge/concurrent/actor/errors.rb,<br /> lib-edge/concurrent/actor/context.rb,<br /> lib-edge/concurrent/actor/envelope.rb,<br /> lib-edge/concurrent/actor/behaviour.rb,<br /> lib-edge/concurrent/actor/reference.rb,<br /> lib-edge/concurrent/actor/type_check.rb,<br /> lib-edge/concurrent/actor/utils/pool.rb,<br /> lib-edge/concurrent/actor/utils/ad_hoc.rb,<br /> lib-edge/concurrent/actor/utils/balancer.rb,<br /> lib-edge/concurrent/actor/utils/broadcast.rb,<br /> lib-edge/concurrent/actor/behaviour/awaits.rb,<br /> lib-edge/concurrent/actor/behaviour/buffer.rb,<br /> lib-edge/concurrent/actor/behaviour/linking.rb,<br /> lib-edge/concurrent/actor/behaviour/pausing.rb,<br /> lib-edge/concurrent/actor/behaviour/abstract.rb,<br /> lib-edge/concurrent/actor/public_delegations.rb,<br /> lib-edge/concurrent/actor/internal_delegations.rb,<br /> lib-edge/concurrent/actor/behaviour/supervising.rb,<br /> lib-edge/concurrent/actor/behaviour/termination.rb,<br /> lib-edge/concurrent/actor/behaviour/sets_results.rb,<br /> lib-edge/concurrent/actor/behaviour/removes_child.rb,<br /> lib-edge/concurrent/actor/behaviour/executes_context.rb,<br /> lib-edge/concurrent/actor/default_dead_letter_handler.rb,<br /> lib-edge/concurrent/actor/behaviour/errors_on_unknown_message.rb</span>
</dd>
</dl>
</div>
<h2>Overview</h2><div class="docstring">
<div class="discussion">
<div class="note notetag">
<strong>Note:</strong>
<div class='inline'><p><strong>Edge Features</strong> are under active development and may change frequently.</p>
<ul>
<li> Deprecations are not added before incompatible changes.</li>
<li> Edge version: <em>major</em> is always 0, <em>minor</em> bump means incompatible change,
<em>patch</em> bump means compatible change.</li>
<li> Edge features may also lack tests and documentation.</li>
<li> Features developed in <code>concurrent-ruby-edge</code> are expected to move
to <code>concurrent-ruby</code> when finalised.</li>
</ul>
</div>
</div>
<p><h1>Actor model</h1>
<ul>
<li> Light-weighted running on thread-pool.</li>
<li> Inspired by Akka and Erlang.</li>
<li> Modular.</li>
</ul>
<p>This Actor model implementation makes actors very cheap to create and discard.
Thousands of actors can be created, allowing you to break the program into smaller
maintainable pieces, without violating the single responsibility principle.</p>
<h2>What is an actor model?</h2>
<p>Actor-based concurrency is all the rage in some circles. Originally described in 1973, the actor model is a paradigm
for creating asynchronous, concurrent objects that is becoming increasingly popular. Much has changed since actors
were first written about four decades ago, which has led to a serious fragmentation within the actor community.
There is <em>no</em> universally accepted, strict definition of "actor" and actor implementations differ widely between
languages and libraries. </p>
<p><a href="http://en.wikipedia.org/wiki/Actor_model">Wiki</a> definition is pretty good:
<em>The actor model in computer science is a mathematical model of concurrent computation
that treats <strong>actors</strong> as the universal primitives of concurrent digital computation:
in response to a message that it receives, an actor can make local decisions,
create more actors, send more messages, and determine how to respond to the next
message received.</em></p>
<h2>Why?</h2>
<p>Concurrency is hard to get right, actors are one of many ways how to simplify the problem.</p>
<h2>Quick example</h2>
<p>An example:</p>
<pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>Counter</span> <span class='op'><</span> <span class='const'><span class='object_link'><a href="../Concurrent.html" title="Concurrent (module)">Concurrent</a></span></span><span class='op'>::</span><span class='const'>Actor</span><span class='op'>::</span><span class='const'><span class='object_link'><a href="Actor/Context.html" title="Concurrent::Actor::Context (class)">Context</a></span></span>
<span class='comment'># Include context of an actor which gives this class access to reference
</span> <span class='comment'># and other information about the actor
</span>
<span class='comment'># use initialize as you wish
</span> <span class='kw'>def</span> <span class='id identifier rubyid_initialize'>initialize</span><span class='lparen'>(</span><span class='id identifier rubyid_initial_value'>initial_value</span><span class='rparen'>)</span>
<span class='ivar'>@count</span> <span class='op'>=</span> <span class='id identifier rubyid_initial_value'>initial_value</span>
<span class='kw'>end</span>
<span class='comment'># override on_message to define actor's behaviour
</span> <span class='kw'>def</span> <span class='id identifier rubyid_on_message'>on_message</span><span class='lparen'>(</span><span class='id identifier rubyid_message'>message</span><span class='rparen'>)</span>
<span class='kw'>if</span> <span class='const'>Integer</span> <span class='op'>===</span> <span class='id identifier rubyid_message'>message</span>
<span class='ivar'>@count</span> <span class='op'>+=</span> <span class='id identifier rubyid_message'>message</span>
<span class='kw'>end</span>
<span class='kw'>end</span>
<span class='kw'>end</span> <span class='comment'>#
</span>
<span class='comment'># Create new actor naming the instance 'first'.
</span><span class='comment'># Return value is a reference to the actor, the actual actor is never returned.
</span><span class='id identifier rubyid_counter'>counter</span> <span class='op'>=</span> <span class='const'>Counter</span><span class='period'>.</span><span class='id identifier rubyid_spawn'>spawn</span><span class='lparen'>(</span><span class='symbol'>:first</span><span class='comma'>,</span> <span class='int'>5</span><span class='rparen'>)</span>
<span class='comment'># Tell a message and forget returning self.
</span><span class='id identifier rubyid_counter'>counter</span><span class='period'>.</span><span class='id identifier rubyid_tell'>tell</span><span class='lparen'>(</span><span class='int'>1</span><span class='rparen'>)</span>
<span class='id identifier rubyid_counter'>counter</span> <span class='op'><<</span> <span class='int'>1</span>
<span class='comment'># (First counter now contains 7.)
</span>
<span class='comment'># Send a messages asking for a result.
</span><span class='id identifier rubyid_counter'>counter</span><span class='period'>.</span><span class='id identifier rubyid_ask'>ask</span><span class='lparen'>(</span><span class='int'>0</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_class'>class</span>
<span class='id identifier rubyid_counter'>counter</span><span class='period'>.</span><span class='id identifier rubyid_ask'>ask</span><span class='lparen'>(</span><span class='int'>0</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_value'>value</span>
</code></pre>
<p><pre class="code ruby"><span class='kw'>class</span> <span class='const'>Adder</span> <span class='op'><</span> <span class='const'><span class='object_link'><a href="../Concurrent.html" title="Concurrent (module)">Concurrent</a></span></span><span class='op'>::</span><span class='const'>Actor</span><span class='op'>::</span><span class='const'><span class='object_link'><a href="Actor/RestartingContext.html" title="Concurrent::Actor::RestartingContext (class)">RestartingContext</a></span></span>
<span class='kw'>def</span> <span class='id identifier rubyid_initialize'>initialize</span><span class='lparen'>(</span><span class='id identifier rubyid_init'>init</span><span class='rparen'>)</span>
<span class='ivar'>@count</span> <span class='op'>=</span> <span class='id identifier rubyid_init'>init</span>
<span class='kw'>end</span>
<span class='kw'>def</span> <span class='id identifier rubyid_on_message'>on_message</span><span class='lparen'>(</span><span class='id identifier rubyid_message'>message</span><span class='rparen'>)</span>
<span class='kw'>case</span> <span class='id identifier rubyid_message'>message</span>
<span class='kw'>when</span> <span class='symbol'>:add</span>
<span class='ivar'>@count</span> <span class='op'>+=</span> <span class='int'>1</span>
<span class='kw'>else</span>
<span class='comment'># pass to ErrorsOnUnknownMessage behaviour, which will just fail
</span> <span class='id identifier rubyid_pass'>pass</span>
<span class='kw'>end</span>
<span class='kw'>end</span>
<span class='kw'>end</span>
<span class='comment'># `link: true` makes the actor linked to root actor and supervised
</span><span class='comment'># which is default behavior
</span><span class='id identifier rubyid_adder'>adder</span> <span class='op'>=</span> <span class='const'>Adder</span><span class='period'>.</span><span class='id identifier rubyid_spawn'>spawn</span><span class='lparen'>(</span><span class='label'>name:</span> <span class='symbol'>:adder</span><span class='comma'>,</span> <span class='label'>link:</span> <span class='kw'>true</span><span class='comma'>,</span> <span class='label'>args:</span> <span class='lbracket'>[</span><span class='int'>1</span><span class='rbracket'>]</span><span class='rparen'>)</span>
<span class='comment'># => #<Concurrent::Actor::Reference:0x7fbedd8e3d40 /adder (Adder)>
</span><span class='id identifier rubyid_adder'>adder</span><span class='period'>.</span><span class='id identifier rubyid_parent'>parent</span>
<span class='comment'># => #<Concurrent::Actor::Reference:0x7fbedbaa1e90 / (Concurrent::Actor::Root)>
</span>
<span class='comment'># tell and forget
</span><span class='id identifier rubyid_adder'>adder</span><span class='period'>.</span><span class='id identifier rubyid_tell'>tell</span><span class='lparen'>(</span><span class='symbol'>:add</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_tell'>tell</span><span class='lparen'>(</span><span class='symbol'>:add</span><span class='rparen'>)</span>
<span class='comment'># => #<Concurrent::Actor::Reference:0x7fbedd8e3d40 /adder (Adder)>
</span><span class='comment'># ask to get result
</span><span class='id identifier rubyid_adder'>adder</span><span class='period'>.</span><span class='id identifier rubyid_ask!'>ask!</span><span class='lparen'>(</span><span class='symbol'>:add</span><span class='rparen'>)</span> <span class='comment'># => 4
</span><span class='comment'># fail the actor
</span><span class='id identifier rubyid_adder'>adder</span><span class='period'>.</span><span class='id identifier rubyid_ask!'>ask!</span><span class='lparen'>(</span><span class='symbol'>:bad</span><span class='rparen'>)</span> <span class='kw'>rescue</span> <span class='gvar'>$!</span>
<span class='comment'># => #<Concurrent::Actor::UnknownMessage: :bad from #<Thread:0x007fbedb8809b8>>
</span><span class='comment'># actor is restarted with initial values
</span><span class='id identifier rubyid_adder'>adder</span><span class='period'>.</span><span class='id identifier rubyid_ask!'>ask!</span><span class='lparen'>(</span><span class='symbol'>:add</span><span class='rparen'>)</span> <span class='comment'># => 2
</span><span class='id identifier rubyid_adder'>adder</span><span class='period'>.</span><span class='id identifier rubyid_ask!'>ask!</span><span class='lparen'>(</span><span class='symbol'>:terminate!</span><span class='rparen'>)</span> <span class='comment'># => true</span></pre></p>
<h2>Spawning actors</h2>
<ul>
<li> <span class='object_link'><a href="#spawn-class_method" title="Concurrent::Actor.spawn (method)">Actor.spawn</a></span> and <span class='object_link'><a href="#spawn!-class_method" title="Concurrent::Actor.spawn! (method)">Actor.spawn!</a></span></li>
<li> <span class='object_link'><a href="Actor/AbstractContext.html#spawn-class_method" title="Concurrent::Actor::AbstractContext.spawn (method)">AbstractContext.spawn</a></span> and <span class='object_link'><a href="Actor/AbstractContext.html#spawn!-class_method" title="Concurrent::Actor::AbstractContext.spawn! (method)">AbstractContext.spawn!</a></span> </li>
</ul>
<h2>Sending messages</h2>
<ul>
<li> <span class='object_link'><a href="Actor/Reference.html#tell-instance_method" title="Concurrent::Actor::Reference#tell (method)">Reference#tell</a></span>
Sends the message asynchronously to the actor and immediately returns
<code>self</code> (the reference) allowing to chain message telling.</li>
<li> <span class='object_link'><a href="Actor/Reference.html#ask-instance_method" title="Concurrent::Actor::Reference#ask (method)">Reference#ask</a></span>
testing and when it returns very shortly. It can lead to deadlock if all threads in
global_io_executor will block on while asking. It's fine to use it form outside of actors and
global_io_executor.</li>
<li> <span class='object_link'><a href="Actor/Reference.html#ask!-instance_method" title="Concurrent::Actor::Reference#ask! (method)">Reference#ask!</a></span>
Sends the message synchronously and blocks until the message
is processed. Raises on error.</li>
</ul>
<p>Messages are processed in same order as they are sent by a sender. It may interleaved with
messages from other senders though.</p>
<h3>Immutability</h3>
<p>Messages sent between actors should be <strong>immutable</strong>. Gems like</p>
<ul>
<li><a href="https://github.com/pitr-ch/algebrick">Algebrick</a> - Typed struct on steroids based on
algebraic types and pattern matching</li>
<li><a href="https://github.com/hamstergem/hamster">Hamster</a> - Efficient, Immutable, Thread-Safe
Collection classes for Ruby</li>
</ul>
<p>are very helpful.</p>
<p><pre class="code ruby"><span class='id identifier rubyid_require'>require</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>algebrick</span><span class='tstring_end'>'</span></span> <span class='comment'># => true
</span>
<span class='comment'># Actor message protocol definition with Algebrick
</span><span class='const'>Protocol</span> <span class='op'>=</span> <span class='const'>Algebrick</span><span class='period'>.</span><span class='id identifier rubyid_type'>type</span> <span class='kw'>do</span>
<span class='id identifier rubyid_variants'>variants</span> <span class='const'>Add</span> <span class='op'>=</span> <span class='id identifier rubyid_type'>type</span> <span class='lbrace'>{</span> <span class='id identifier rubyid_fields!'>fields!</span> <span class='label'>a:</span> <span class='const'>Numeric</span><span class='comma'>,</span> <span class='label'>b:</span> <span class='const'>Numeric</span> <span class='rbrace'>}</span><span class='comma'>,</span>
<span class='const'>Subtract</span> <span class='op'>=</span> <span class='id identifier rubyid_type'>type</span> <span class='lbrace'>{</span> <span class='id identifier rubyid_fields!'>fields!</span> <span class='label'>a:</span> <span class='const'>Numeric</span><span class='comma'>,</span> <span class='label'>b:</span> <span class='const'>Numeric</span> <span class='rbrace'>}</span>
<span class='kw'>end</span> <span class='comment'># => Protocol(Add | Subtract)
</span>
<span class='kw'>class</span> <span class='const'>Calculator</span> <span class='op'><</span> <span class='const'><span class='object_link'><a href="../Concurrent.html" title="Concurrent (module)">Concurrent</a></span></span><span class='op'>::</span><span class='const'>Actor</span><span class='op'>::</span><span class='const'><span class='object_link'><a href="Actor/RestartingContext.html" title="Concurrent::Actor::RestartingContext (class)">RestartingContext</a></span></span>
<span class='id identifier rubyid_include'>include</span> <span class='const'>Algebrick</span><span class='op'>::</span><span class='const'>Matching</span>
<span class='kw'>def</span> <span class='id identifier rubyid_on_message'>on_message</span><span class='lparen'>(</span><span class='id identifier rubyid_message'>message</span><span class='rparen'>)</span>
<span class='comment'># pattern matching on the message with deconstruction
</span> <span class='comment'># ~ marks values which are passed to the block
</span> <span class='id identifier rubyid_match'>match</span> <span class='id identifier rubyid_message'>message</span><span class='comma'>,</span>
<span class='lparen'>(</span><span class='id identifier rubyid_on'>on</span> <span class='const'>Add</span><span class='period'>.</span><span class='lparen'>(</span><span class='op'>~</span><span class='id identifier rubyid_any'>any</span><span class='comma'>,</span> <span class='op'>~</span><span class='id identifier rubyid_any'>any</span><span class='rparen'>)</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_a'>a</span><span class='comma'>,</span> <span class='id identifier rubyid_b'>b</span><span class='op'>|</span>
<span class='id identifier rubyid_a'>a</span> <span class='op'>+</span> <span class='id identifier rubyid_b'>b</span>
<span class='kw'>end</span><span class='rparen'>)</span><span class='comma'>,</span>
<span class='comment'># or using multi-assignment
</span> <span class='lparen'>(</span><span class='id identifier rubyid_on'>on</span> <span class='op'>~</span><span class='const'>Subtract</span> <span class='kw'>do</span> <span class='op'>|</span><span class='lparen'>(</span><span class='id identifier rubyid_a'>a</span><span class='comma'>,</span> <span class='id identifier rubyid_b'>b</span><span class='rparen'>)</span><span class='op'>|</span>
<span class='id identifier rubyid_a'>a</span> <span class='op'>-</span> <span class='id identifier rubyid_b'>b</span>
<span class='kw'>end</span><span class='rparen'>)</span>
<span class='kw'>end</span>
<span class='kw'>end</span>
<span class='id identifier rubyid_calculator'>calculator</span> <span class='op'>=</span> <span class='const'>Calculator</span><span class='period'>.</span><span class='id identifier rubyid_spawn'>spawn</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>calculator</span><span class='tstring_end'>'</span></span><span class='rparen'>)</span>
<span class='comment'># => #<Concurrent::Actor::Reference:0x7fbedba52d90 /calculator (Calculator)>
</span><span class='id identifier rubyid_addition'>addition</span> <span class='op'>=</span> <span class='id identifier rubyid_calculator'>calculator</span><span class='period'>.</span><span class='id identifier rubyid_ask'>ask</span> <span class='const'>Add</span><span class='lbracket'>[</span><span class='int'>1</span><span class='comma'>,</span> <span class='int'>2</span><span class='rbracket'>]</span>
<span class='comment'># => <#Concurrent::Promises::Future:0x7fbedc05f7b0 pending>
</span><span class='id identifier rubyid_subtraction'>subtraction</span> <span class='op'>=</span> <span class='id identifier rubyid_calculator'>calculator</span><span class='period'>.</span><span class='id identifier rubyid_ask'>ask</span> <span class='const'>Subtract</span><span class='lbracket'>[</span><span class='int'>1</span><span class='comma'>,</span> <span class='float'>0.5</span><span class='rbracket'>]</span>
<span class='comment'># => <#Concurrent::Promises::Future:0x7fbedd891388 pending>
</span><span class='id identifier rubyid_results'>results</span> <span class='op'>=</span> <span class='lparen'>(</span><span class='id identifier rubyid_addition'>addition</span> <span class='op'>&</span> <span class='id identifier rubyid_subtraction'>subtraction</span><span class='rparen'>)</span>
<span class='comment'># => <#Concurrent::Promises::Future:0x7fbedc04eeb0 pending>
</span><span class='id identifier rubyid_results'>results</span><span class='period'>.</span><span class='id identifier rubyid_value!'>value!</span> <span class='comment'># => [3, 0.5]
</span>
<span class='id identifier rubyid_calculator'>calculator</span><span class='period'>.</span><span class='id identifier rubyid_ask!'>ask!</span> <span class='symbol'>:terminate!</span> <span class='comment'># => true</span></pre></p>
<h2>Actor definition</h2>
<p>New actor is defined by subclassing <span class='object_link'><a href="Actor/RestartingContext.html" title="Concurrent::Actor::RestartingContext (class)">RestartingContext</a></span>, <span class='object_link'><a href="Actor/Context.html" title="Concurrent::Actor::Context (class)">Context</a></span> and defining its abstract methods.
<span class='object_link'><a href="Actor/AbstractContext.html" title="Concurrent::Actor::AbstractContext (class)">AbstractContext</a></span> can be subclassed directly to implement more specific behaviour see <span class='object_link'><a href="Actor/Root.html" title="Concurrent::Actor::Root (class)">Root</a></span> implementation.</p>
<ul>
<li><p><span class='object_link'><a href="Actor/Context.html" title="Concurrent::Actor::Context (class)">Context</a></span></p>
<blockquote>
<p>Basic Context of an Actor. It supports only linking and it simply terminates on error.
Uses <span class='object_link'><a href="Actor/Behaviour.html#basic_behaviour_definition-class_method" title="Concurrent::Actor::Behaviour.basic_behaviour_definition (method)">Behaviour.basic_behaviour_definition</a></span>:</p>
</blockquote></li>
<li><p><span class='object_link'><a href="Actor/RestartingContext.html" title="Concurrent::Actor::RestartingContext (class)">RestartingContext</a></span>.</p>
<blockquote>
<p>Context of an Actor for robust systems. It supports supervision, linking, pauses on error.
Uses <span class='object_link'><a href="Actor/Behaviour.html#restarting_behaviour_definition-class_method" title="Concurrent::Actor::Behaviour.restarting_behaviour_definition (method)">Behaviour.restarting_behaviour_definition</a></span></p>
</blockquote></li>
</ul>
<p>Example of ac actor definition:</p>
<p><pre class="code ruby"><span class='const'>Message</span> <span class='op'>=</span> <span class='const'>Struct</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span> <span class='symbol'>:action</span><span class='comma'>,</span> <span class='symbol'>:value</span>
<span class='kw'>class</span> <span class='const'>AnActor</span> <span class='op'><</span> <span class='const'><span class='object_link'><a href="../Concurrent.html" title="Concurrent (module)">Concurrent</a></span></span><span class='op'>::</span><span class='const'>Actor</span><span class='op'>::</span><span class='const'><span class='object_link'><a href="Actor/RestartingContext.html" title="Concurrent::Actor::RestartingContext (class)">RestartingContext</a></span></span>
<span class='kw'>def</span> <span class='id identifier rubyid_initialize'>initialize</span><span class='lparen'>(</span><span class='id identifier rubyid_init'>init</span><span class='rparen'>)</span>
<span class='ivar'>@counter</span> <span class='op'>=</span> <span class='id identifier rubyid_init'>init</span>
<span class='kw'>end</span>
<span class='comment'># override #on_message to define actor's behaviour on message received
</span> <span class='kw'>def</span> <span class='id identifier rubyid_on_message'>on_message</span><span class='lparen'>(</span><span class='id identifier rubyid_message'>message</span><span class='rparen'>)</span>
<span class='kw'>case</span> <span class='id identifier rubyid_message'>message</span><span class='period'>.</span><span class='id identifier rubyid_action'>action</span>
<span class='kw'>when</span> <span class='symbol'>:add</span>
<span class='ivar'>@counter</span> <span class='op'>=</span> <span class='ivar'>@counter</span> <span class='op'>+</span> <span class='id identifier rubyid_message'>message</span><span class='period'>.</span><span class='id identifier rubyid_value'>value</span>
<span class='kw'>when</span> <span class='symbol'>:subtract</span>
<span class='ivar'>@counter</span> <span class='op'>=</span> <span class='ivar'>@counter</span> <span class='op'>-</span> <span class='id identifier rubyid_message'>message</span><span class='period'>.</span><span class='id identifier rubyid_value'>value</span>
<span class='kw'>when</span> <span class='symbol'>:value</span>
<span class='ivar'>@counter</span>
<span class='kw'>else</span>
<span class='id identifier rubyid_pass'>pass</span>
<span class='kw'>end</span>
<span class='kw'>end</span>
<span class='comment'># set counter to zero when there is an error
</span> <span class='kw'>def</span> <span class='id identifier rubyid_on_event'>on_event</span><span class='lparen'>(</span><span class='id identifier rubyid_event'>event</span><span class='rparen'>)</span>
<span class='kw'>if</span> <span class='id identifier rubyid_event'>event</span> <span class='op'>==</span> <span class='symbol'>:reset</span>
<span class='ivar'>@counter</span> <span class='op'>=</span> <span class='int'>0</span> <span class='comment'># ignore initial value
</span> <span class='kw'>end</span>
<span class='kw'>end</span>
<span class='kw'>end</span>
<span class='id identifier rubyid_an_actor'>an_actor</span> <span class='op'>=</span> <span class='const'>AnActor</span><span class='period'>.</span><span class='id identifier rubyid_spawn'>spawn</span> <span class='label'>name:</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>an_actor</span><span class='tstring_end'>'</span></span><span class='comma'>,</span> <span class='label'>args:</span> <span class='int'>10</span>
<span class='id identifier rubyid_an_actor'>an_actor</span> <span class='op'><<</span> <span class='const'>Message</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span><span class='lparen'>(</span><span class='symbol'>:add</span><span class='comma'>,</span> <span class='int'>1</span><span class='rparen'>)</span> <span class='op'><<</span> <span class='const'>Message</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span><span class='lparen'>(</span><span class='symbol'>:subtract</span><span class='comma'>,</span> <span class='int'>2</span><span class='rparen'>)</span>
<span class='id identifier rubyid_an_actor'>an_actor</span><span class='period'>.</span><span class='id identifier rubyid_ask!'>ask!</span><span class='lparen'>(</span><span class='const'>Message</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span><span class='lparen'>(</span><span class='symbol'>:value</span><span class='comma'>,</span> <span class='kw'>nil</span><span class='rparen'>)</span><span class='rparen'>)</span> <span class='comment'># => 9
</span><span class='id identifier rubyid_an_actor'>an_actor</span> <span class='op'><<</span> <span class='symbol'>:boo</span> <span class='op'><<</span> <span class='const'>Message</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span><span class='lparen'>(</span><span class='symbol'>:add</span><span class='comma'>,</span> <span class='int'>1</span><span class='rparen'>)</span>
<span class='id identifier rubyid_an_actor'>an_actor</span><span class='period'>.</span><span class='id identifier rubyid_ask!'>ask!</span><span class='lparen'>(</span><span class='const'>Message</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span><span class='lparen'>(</span><span class='symbol'>:value</span><span class='comma'>,</span> <span class='kw'>nil</span><span class='rparen'>)</span><span class='rparen'>)</span> <span class='comment'># => 1
</span><span class='id identifier rubyid_an_actor'>an_actor</span> <span class='op'><<</span> <span class='symbol'>:terminate!</span>
<span class='comment'># => #<Concurrent::Actor::Reference:0x7fbedc137688 /an_actor (AnActor)></span></pre></p>
<p>See methods of <span class='object_link'><a href="Actor/AbstractContext.html" title="Concurrent::Actor::AbstractContext (class)">AbstractContext</a></span> what else can be tweaked, e.g <span class='object_link'><a href="Actor/AbstractContext.html#default_reference_class-instance_method" title="Concurrent::Actor::AbstractContext#default_reference_class (method)">AbstractContext#default_reference_class</a></span></p>
<h2>Reference</h2>
<p><span class='object_link'><a href="Actor/Reference.html" title="Concurrent::Actor::Reference (class)">Reference</a></span> is public interface of Actor instances. It is used for sending messages and can
be freely passed around the application. It also provides some basic information about the actor,
see <span class='object_link'><a href="Actor/PublicDelegations.html" title="Concurrent::Actor::PublicDelegations (module)">PublicDelegations</a></span>.</p>
<pre class="code ruby"><code class="ruby"><span class='const'>AdHoc</span><span class='period'>.</span><span class='id identifier rubyid_spawn'>spawn</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>printer</span><span class='tstring_end'>'</span></span><span class='rparen'>)</span> <span class='lbrace'>{</span> <span class='tlambda'>-></span> <span class='id identifier rubyid_message'>message</span> <span class='tlambeg'>{</span> <span class='id identifier rubyid_puts'>puts</span> <span class='id identifier rubyid_message'>message</span> <span class='rbrace'>}</span> <span class='rbrace'>}</span>
<span class='comment'># => #<Concurrent::Actor::Reference:0x7fd0d2883218 /printer (Concurrent::Actor::Utils::AdHoc)>
</span><span class='comment'># ^object_id ^path ^context class
</span></code></pre>
</p>
<h2>Garbage collection</h2>
<p>Spawned actor cannot be garbage-collected until it's terminated. There is a reference held in the parent actor.</p>
<h2>Parent-child relationship, name, and path</h2>
<ul>
<li> <span class='object_link'><a href="Actor/Core.html#name-instance_method" title="Concurrent::Actor::Core#name (method)">Core#name</a></span>
The name of actor instance, it should be uniq (not enforced). Allows easier orientation
between actor instances.</li>
<li> <span class='object_link'><a href="Actor/Core.html#path-instance_method" title="Concurrent::Actor::Core#path (method)">Core#path</a></span>
Path of this actor. It is used for easier orientation and logging.
Path is constructed recursively with: <code>parent.path + self.name</code> up to a <span class='object_link'><a href="#root-class_method" title="Concurrent::Actor.root (method)">Actor.root</a></span>,
e.g. <code>/an_actor/its_child</code>.</li>
<li> <span class='object_link'><a href="Actor/Core.html#parent-instance_method" title="Concurrent::Actor::Core#parent (method)">Core#parent</a></span>
A parent Actor. When actor is spawned the <span class='object_link'><a href="#current-class_method" title="Concurrent::Actor.current (method)">Actor.current</a></span> becomes its parent.
When actor is spawned from a thread outside of an actor (<span class='object_link'><a href="#current-class_method" title="Concurrent::Actor.current (method)">Actor.current</a></span> is nil) <span class='object_link'><a href="#root-class_method" title="Concurrent::Actor.root (method)">Actor.root</a></span> is assigned. </li>
</ul>
<h2>Behaviour</h2>
<p>Actors have modular architecture, which is achieved by combining a light core with chain of
behaviours. Each message or internal event propagates through the chain allowing the
behaviours react based on their responsibility.</p>
<ul>
<li><p><span class='object_link'><a href="Actor/Behaviour/Linking.html" title="Concurrent::Actor::Behaviour::Linking (class)">Behaviour::Linking</a></span>:</p>
<blockquote>
<p>Links the actor to other actors and sends actor's events to them,
like: <code>:terminated</code>, <code>:paused</code>, <code>:resumed</code>, errors, etc.
Linked actor needs to handle those messages.</p>
<pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_listener'>listener</span> <span class='op'>=</span> <span class='const'>AdHoc</span><span class='period'>.</span><span class='id identifier rubyid_spawn'>spawn</span> <span class='label'>name:</span> <span class='symbol'>:listener</span> <span class='kw'>do</span>
<span class='id identifier rubyid_lambda'>lambda</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_message'>message</span><span class='op'>|</span>
<span class='kw'>case</span> <span class='id identifier rubyid_message'>message</span>
<span class='kw'>when</span> <span class='const'><span class='object_link'><a href="Actor/Reference.html" title="Concurrent::Actor::Reference (class)">Reference</a></span></span>
<span class='kw'>if</span> <span class='id identifier rubyid_message'>message</span><span class='period'>.</span><span class='id identifier rubyid_ask!'>ask!</span><span class='lparen'>(</span><span class='symbol'>:linked?</span><span class='rparen'>)</span>
<span class='id identifier rubyid_message'>message</span> <span class='op'><<</span> <span class='symbol'>:unlink</span>
<span class='kw'>else</span>
<span class='id identifier rubyid_message'>message</span> <span class='op'><<</span> <span class='symbol'>:link</span>
<span class='kw'>end</span>
<span class='kw'>else</span>
<span class='id identifier rubyid_puts'>puts</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>got event </span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_message'>message</span><span class='period'>.</span><span class='id identifier rubyid_inspect'>inspect</span><span class='embexpr_end'>}</span><span class='tstring_content'> from </span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_envelope'>envelope</span><span class='period'>.</span><span class='id identifier rubyid_sender'>sender</span><span class='embexpr_end'>}</span><span class='tstring_end'>"</span></span>
<span class='kw'>end</span>
<span class='kw'>end</span>
<span class='kw'>end</span>
<span class='id identifier rubyid_an_actor'>an_actor</span> <span class='op'>=</span> <span class='const'>AdHoc</span><span class='period'>.</span><span class='id identifier rubyid_spawn'>spawn</span> <span class='label'>name:</span> <span class='symbol'>:an_actor</span><span class='comma'>,</span> <span class='label'>supervise:</span> <span class='kw'>true</span><span class='comma'>,</span> <span class='label'>behaviour_definition:</span> <span class='const'><span class='object_link'><a href="Actor/Behaviour.html" title="Concurrent::Actor::Behaviour (module)">Behaviour</a></span></span><span class='period'>.</span><span class='id identifier rubyid_restarting_behaviour_definition'><span class='object_link'><a href="Actor/Behaviour.html#restarting_behaviour_definition-class_method" title="Concurrent::Actor::Behaviour.restarting_behaviour_definition (method)">restarting_behaviour_definition</a></span></span> <span class='kw'>do</span>
<span class='id identifier rubyid_lambda'>lambda</span> <span class='lbrace'>{</span> <span class='op'>|</span><span class='id identifier rubyid_message'>message</span><span class='op'>|</span> <span class='id identifier rubyid_raise'>raise</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>failed</span><span class='tstring_end'>'</span></span><span class='rbrace'>}</span>
<span class='kw'>end</span>
<span class='comment'># link the actor
</span><span class='id identifier rubyid_listener'>listener</span><span class='period'>.</span><span class='id identifier rubyid_ask'>ask</span><span class='lparen'>(</span><span class='id identifier rubyid_an_actor'>an_actor</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_wait'>wait</span>
<span class='id identifier rubyid_an_actor'>an_actor</span><span class='period'>.</span><span class='id identifier rubyid_ask'>ask</span><span class='lparen'>(</span><span class='symbol'>:fail</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_wait'>wait</span>
<span class='comment'># unlink the actor
</span><span class='id identifier rubyid_listener'>listener</span><span class='period'>.</span><span class='id identifier rubyid_ask'>ask</span><span class='lparen'>(</span><span class='id identifier rubyid_an_actor'>an_actor</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_wait'>wait</span>
<span class='id identifier rubyid_an_actor'>an_actor</span><span class='period'>.</span><span class='id identifier rubyid_ask'>ask</span><span class='lparen'>(</span><span class='symbol'>:fail</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_wait'>wait</span>
<span class='id identifier rubyid_an_actor'>an_actor</span> <span class='op'><<</span> <span class='symbol'>:terminate!</span>
</code></pre>
<p>produces only two events, other events happened after unlinking</p>
<pre class="code ruby"><code class="ruby">got event #<RuntimeError: failed> from #<Concurrent::Actor::Reference /an_actor (Concurrent::Actor::Utils::AdHoc)>
got event :reset from #<Concurrent::Actor::Reference /an_actor (Concurrent::Actor::Utils::AdHoc)>
</code></pre>
</p>
</blockquote></li>
<li><p><span class='object_link'><a href="Actor/Behaviour/Awaits.html" title="Concurrent::Actor::Behaviour::Awaits (class)">Behaviour::Awaits</a></span>:</p>
<blockquote>
<p>Accepts <code>:await</code> messages. Which allows to wait on Actor to process all previously send
messages.</p>
<pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_actor'>actor</span> <span class='op'><<</span> <span class='symbol'>:a</span> <span class='op'><<</span> <span class='symbol'>:b</span>
<span class='id identifier rubyid_actor'>actor</span><span class='period'>.</span><span class='id identifier rubyid_ask'>ask</span><span class='lparen'>(</span><span class='symbol'>:await</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_wait'>wait</span> <span class='comment'># blocks until :a and :b are processed
</span></code></pre>
</p>
</blockquote></li>
<li><p><span class='object_link'><a href="Actor/Behaviour/Pausing.html" title="Concurrent::Actor::Behaviour::Pausing (class)">Behaviour::Pausing</a></span>:</p>
<blockquote>
<p>Allows to pause actors on errors.
When paused all arriving messages are collected and processed after the actor
is resumed or reset. Resume will simply continue with next message.
Reset also reinitialized context.</p>
</blockquote></li>
<li><p><span class='object_link'><a href="Actor/Behaviour/Supervising.html" title="Concurrent::Actor::Behaviour::Supervising (class)">Behaviour::Supervising</a></span>:</p>
<blockquote>
<p>Handles supervised actors. Handle configures what to do with failed child: :terminate!, :resume!, :reset!,
or :restart!. Strategy sets :one_for_one (restarts just failed actor) or :one_for_all (restarts all child actors).</p>
</blockquote></li>
<li><p><span class='object_link'><a href="Actor/Behaviour/Supervising.html" title="Concurrent::Actor::Behaviour::Supervising (class)">Behaviour::Supervising</a></span>:</p>
<blockquote>
<p>Handles supervised actors. Handle configures what to do with failed child: :terminate!, :resume!, :reset!,
or :restart!. Strategy sets :one_for_one (restarts just failed actor) or :one_for_all (restarts all child actors).</p>
</blockquote></li>
<li><p><span class='object_link'><a href="Actor/Behaviour/ExecutesContext.html" title="Concurrent::Actor::Behaviour::ExecutesContext (class)">Behaviour::ExecutesContext</a></span>:</p>
<blockquote>
<p>Delegates messages and events to <span class='object_link'><a href="Actor/AbstractContext.html" title="Concurrent::Actor::AbstractContext (class)">AbstractContext</a></span> instance.</p>
</blockquote></li>
<li><p><span class='object_link'><a href="Actor/Behaviour/ErrorsOnUnknownMessage.html" title="Concurrent::Actor::Behaviour::ErrorsOnUnknownMessage (class)">Behaviour::ErrorsOnUnknownMessage</a></span>:</p>
<blockquote>
<p>Simply fails when message arrives here. It's usually the last behaviour.</p>
</blockquote></li>
<li><p><span class='object_link'><a href="Actor/Behaviour/Termination.html" title="Concurrent::Actor::Behaviour::Termination (class)">Behaviour::Termination</a></span>:</p>
<blockquote>
<p>Handles actor termination. Waits until all its children are terminated,
can be configured on behaviour initialization.</p>
</blockquote></li>
<li><p><span class='object_link'><a href="Actor/Behaviour/RemovesChild.html" title="Concurrent::Actor::Behaviour::RemovesChild (class)">Behaviour::RemovesChild</a></span>:</p>
<blockquote>
<p>Removes terminated children.</p>
</blockquote></li>
</ul>
<p>If needed new behaviours can be added, or old one removed to get required behaviour.</p>
<ul>
<li><p><span class='object_link'><a href="Actor/Context.html" title="Concurrent::Actor::Context (class)">Context</a></span> uses
Array of behaviours and their construction parameters.</p>
<pre class="code ruby"><code class="ruby"><span class='lbracket'>[</span><span class='lbracket'>[</span><span class='const'><span class='object_link'><a href="Actor/Behaviour.html" title="Concurrent::Actor::Behaviour (module)">Behaviour</a></span></span><span class='op'>::</span><span class='const'><span class='object_link'><a href="Actor/Behaviour/SetResults.html" title="Concurrent::Actor::Behaviour::SetResults (class)">SetResults</a></span></span><span class='comma'>,</span> <span class='symbol'>:terminate!</span><span class='rbracket'>]</span><span class='comma'>,</span>
<span class='lbracket'>[</span><span class='const'><span class='object_link'><a href="Actor/Behaviour.html" title="Concurrent::Actor::Behaviour (module)">Behaviour</a></span></span><span class='op'>::</span><span class='const'><span class='object_link'><a href="Actor/Behaviour/RemovesChild.html" title="Concurrent::Actor::Behaviour::RemovesChild (class)">RemovesChild</a></span></span><span class='rbracket'>]</span><span class='comma'>,</span>
<span class='lbracket'>[</span><span class='const'><span class='object_link'><a href="Actor/Behaviour.html" title="Concurrent::Actor::Behaviour (module)">Behaviour</a></span></span><span class='op'>::</span><span class='const'><span class='object_link'><a href="Actor/Behaviour/Termination.html" title="Concurrent::Actor::Behaviour::Termination (class)">Termination</a></span></span><span class='rbracket'>]</span><span class='comma'>,</span>
<span class='lbracket'>[</span><span class='const'><span class='object_link'><a href="Actor/Behaviour.html" title="Concurrent::Actor::Behaviour (module)">Behaviour</a></span></span><span class='op'>::</span><span class='const'><span class='object_link'><a href="Actor/Behaviour/Linking.html" title="Concurrent::Actor::Behaviour::Linking (class)">Linking</a></span></span><span class='rbracket'>]</span><span class='comma'>,</span>
<span class='lbracket'>[</span><span class='const'><span class='object_link'><a href="Actor/Behaviour.html" title="Concurrent::Actor::Behaviour (module)">Behaviour</a></span></span><span class='op'>::</span><span class='const'><span class='object_link'><a href="Actor/Behaviour/Awaits.html" title="Concurrent::Actor::Behaviour::Awaits (class)">Awaits</a></span></span><span class='rbracket'>]</span><span class='comma'>,</span>
<span class='lbracket'>[</span><span class='const'><span class='object_link'><a href="Actor/Behaviour.html" title="Concurrent::Actor::Behaviour (module)">Behaviour</a></span></span><span class='op'>::</span><span class='const'><span class='object_link'><a href="Actor/Behaviour/ExecutesContext.html" title="Concurrent::Actor::Behaviour::ExecutesContext (class)">ExecutesContext</a></span></span><span class='rbracket'>]</span><span class='comma'>,</span>
<span class='lbracket'>[</span><span class='const'><span class='object_link'><a href="Actor/Behaviour.html" title="Concurrent::Actor::Behaviour (module)">Behaviour</a></span></span><span class='op'>::</span><span class='const'><span class='object_link'><a href="Actor/Behaviour/ErrorsOnUnknownMessage.html" title="Concurrent::Actor::Behaviour::ErrorsOnUnknownMessage (class)">ErrorsOnUnknownMessage</a></span></span><span class='rbracket'>]</span><span class='rbracket'>]</span>
</code></pre>
</p></li>
<li><p><span class='object_link'><a href="Actor/RestartingContext.html" title="Concurrent::Actor::RestartingContext (class)">RestartingContext</a></span> uses
Array of behaviours and their construction parameters.</p>
<pre class="code ruby"><code class="ruby"><span class='lbracket'>[</span><span class='lbracket'>[</span><span class='const'><span class='object_link'><a href="Actor/Behaviour.html" title="Concurrent::Actor::Behaviour (module)">Behaviour</a></span></span><span class='op'>::</span><span class='const'><span class='object_link'><a href="Actor/Behaviour/SetResults.html" title="Concurrent::Actor::Behaviour::SetResults (class)">SetResults</a></span></span><span class='comma'>,</span> <span class='symbol'>:pause!</span><span class='rbracket'>]</span><span class='comma'>,</span>
<span class='lbracket'>[</span><span class='const'><span class='object_link'><a href="Actor/Behaviour.html" title="Concurrent::Actor::Behaviour (module)">Behaviour</a></span></span><span class='op'>::</span><span class='const'><span class='object_link'><a href="Actor/Behaviour/RemovesChild.html" title="Concurrent::Actor::Behaviour::RemovesChild (class)">RemovesChild</a></span></span><span class='rbracket'>]</span><span class='comma'>,</span>
<span class='lbracket'>[</span><span class='const'><span class='object_link'><a href="Actor/Behaviour.html" title="Concurrent::Actor::Behaviour (module)">Behaviour</a></span></span><span class='op'>::</span><span class='const'><span class='object_link'><a href="Actor/Behaviour/Termination.html" title="Concurrent::Actor::Behaviour::Termination (class)">Termination</a></span></span><span class='rbracket'>]</span><span class='comma'>,</span>
<span class='lbracket'>[</span><span class='const'><span class='object_link'><a href="Actor/Behaviour.html" title="Concurrent::Actor::Behaviour (module)">Behaviour</a></span></span><span class='op'>::</span><span class='const'><span class='object_link'><a href="Actor/Behaviour/Linking.html" title="Concurrent::Actor::Behaviour::Linking (class)">Linking</a></span></span><span class='rbracket'>]</span><span class='comma'>,</span>
<span class='lbracket'>[</span><span class='const'><span class='object_link'><a href="Actor/Behaviour.html" title="Concurrent::Actor::Behaviour (module)">Behaviour</a></span></span><span class='op'>::</span><span class='const'><span class='object_link'><a href="Actor/Behaviour/Pausing.html" title="Concurrent::Actor::Behaviour::Pausing (class)">Pausing</a></span></span><span class='rbracket'>]</span><span class='comma'>,</span>
<span class='lbracket'>[</span><span class='const'><span class='object_link'><a href="Actor/Behaviour.html" title="Concurrent::Actor::Behaviour (module)">Behaviour</a></span></span><span class='op'>::</span><span class='const'><span class='object_link'><a href="Actor/Behaviour/Supervising.html" title="Concurrent::Actor::Behaviour::Supervising (class)">Supervising</a></span></span><span class='comma'>,</span> <span class='symbol'>:reset!</span><span class='comma'>,</span> <span class='symbol'>:one_for_one</span><span class='rbracket'>]</span><span class='comma'>,</span>
<span class='lbracket'>[</span><span class='const'><span class='object_link'><a href="Actor/Behaviour.html" title="Concurrent::Actor::Behaviour (module)">Behaviour</a></span></span><span class='op'>::</span><span class='const'><span class='object_link'><a href="Actor/Behaviour/Awaits.html" title="Concurrent::Actor::Behaviour::Awaits (class)">Awaits</a></span></span><span class='rbracket'>]</span><span class='comma'>,</span>
<span class='lbracket'>[</span><span class='const'><span class='object_link'><a href="Actor/Behaviour.html" title="Concurrent::Actor::Behaviour (module)">Behaviour</a></span></span><span class='op'>::</span><span class='const'><span class='object_link'><a href="Actor/Behaviour/ExecutesContext.html" title="Concurrent::Actor::Behaviour::ExecutesContext (class)">ExecutesContext</a></span></span><span class='rbracket'>]</span><span class='comma'>,</span>
<span class='lbracket'>[</span><span class='const'><span class='object_link'><a href="Actor/Behaviour.html" title="Concurrent::Actor::Behaviour (module)">Behaviour</a></span></span><span class='op'>::</span><span class='const'><span class='object_link'><a href="Actor/Behaviour/ErrorsOnUnknownMessage.html" title="Concurrent::Actor::Behaviour::ErrorsOnUnknownMessage (class)">ErrorsOnUnknownMessage</a></span></span><span class='rbracket'>]</span><span class='rbracket'>]</span>
</code></pre>
</p></li>
</ul>
</p>
<h2>IO cooperation</h2>
<p>Actors are running on shared thread poll which allows user to create many actors cheaply.
Downside is that these actors cannot be directly used to do IO or other blocking operations.
Blocking operations could starve the <code>default_task_pool</code>. However there are two options:</p>
<ul>
<li>Create an regular actor which will schedule blocking operations in <code>global_operation_pool</code>
(which is intended for blocking operations) sending results back to self in messages.</li>
<li>Create an actor using <code>global_operation_pool</code> instead of <code>global_task_pool</code>, e.g.
<code>AnIOActor.spawn name: :blocking, executor: Concurrent.configuration.global_operation_pool</code>.</li>
</ul>
<h3>Example</h3>
<p><pre class="code ruby"><span class='id identifier rubyid_require'>require</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>concurrent</span><span class='tstring_end'>'</span></span> <span class='comment'># => false
</span>
<span class='comment'># logger = Logger.new(STDOUT)
</span><span class='comment'># Concurrent.configuration.logger = logger.method(:add)
</span>
<span class='comment'># First option is to use operation pool
</span>
<span class='kw'>class</span> <span class='const'>ActorDoingIO</span> <span class='op'><</span> <span class='const'><span class='object_link'><a href="../Concurrent.html" title="Concurrent (module)">Concurrent</a></span></span><span class='op'>::</span><span class='const'>Actor</span><span class='op'>::</span><span class='const'><span class='object_link'><a href="Actor/RestartingContext.html" title="Concurrent::Actor::RestartingContext (class)">RestartingContext</a></span></span>
<span class='kw'>def</span> <span class='id identifier rubyid_on_message'>on_message</span><span class='lparen'>(</span><span class='id identifier rubyid_message'>message</span><span class='rparen'>)</span>
<span class='comment'># do IO operation
</span> <span class='kw'>end</span>
<span class='kw'>def</span> <span class='id identifier rubyid_default_executor'>default_executor</span>
<span class='const'><span class='object_link'><a href="../Concurrent.html" title="Concurrent (module)">Concurrent</a></span></span><span class='period'>.</span><span class='id identifier rubyid_global_io_executor'><span class='object_link'><a href="../Concurrent.html#global_io_executor-class_method" title="Concurrent.global_io_executor (method)">global_io_executor</a></span></span>
<span class='kw'>end</span>
<span class='kw'>end</span>
<span class='id identifier rubyid_actor_doing_io'>actor_doing_io</span> <span class='op'>=</span> <span class='const'>ActorDoingIO</span><span class='period'>.</span><span class='id identifier rubyid_spawn'>spawn</span> <span class='symbol'>:actor_doing_io</span>
<span class='comment'># => #<Concurrent::Actor::Reference:0x7fbedc146e80 /actor_doing_io (ActorDoingIO)>
</span><span class='id identifier rubyid_actor_doing_io'>actor_doing_io</span><span class='period'>.</span><span class='id identifier rubyid_executor'>executor</span> <span class='op'>==</span> <span class='const'><span class='object_link'><a href="../Concurrent.html" title="Concurrent (module)">Concurrent</a></span></span><span class='period'>.</span><span class='id identifier rubyid_global_io_executor'><span class='object_link'><a href="../Concurrent.html#global_io_executor-class_method" title="Concurrent.global_io_executor (method)">global_io_executor</a></span></span>
<span class='comment'># => true
</span>
<span class='comment'># It can be also built into a pool so there is not too many IO operations
</span>
<span class='kw'>class</span> <span class='const'>IOWorker</span> <span class='op'><</span> <span class='const'><span class='object_link'><a href="../Concurrent.html" title="Concurrent (module)">Concurrent</a></span></span><span class='op'>::</span><span class='const'>Actor</span><span class='op'>::</span><span class='const'><span class='object_link'><a href="Actor/Context.html" title="Concurrent::Actor::Context (class)">Context</a></span></span>
<span class='kw'>def</span> <span class='id identifier rubyid_on_message'>on_message</span><span class='lparen'>(</span><span class='id identifier rubyid_io_job'>io_job</span><span class='rparen'>)</span>
<span class='comment'># do IO work
</span> <span class='id identifier rubyid_sleep'>sleep</span> <span class='float'>0.1</span>
<span class='id identifier rubyid_puts'>puts</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_path'>path</span><span class='embexpr_end'>}</span><span class='tstring_content'> second:</span><span class='embexpr_beg'>#{</span><span class='lparen'>(</span><span class='const'>Time</span><span class='period'>.</span><span class='id identifier rubyid_now'>now</span><span class='period'>.</span><span class='id identifier rubyid_to_f'>to_f</span><span class='op'>*</span><span class='int'>100</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_floor'>floor</span><span class='embexpr_end'>}</span><span class='tstring_content'> message:</span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_io_job'>io_job</span><span class='embexpr_end'>}</span><span class='tstring_end'>"</span></span>
<span class='kw'>end</span>
<span class='kw'>def</span> <span class='id identifier rubyid_default_executor'>default_executor</span>
<span class='const'><span class='object_link'><a href="../Concurrent.html" title="Concurrent (module)">Concurrent</a></span></span><span class='period'>.</span><span class='id identifier rubyid_global_io_executor'><span class='object_link'><a href="../Concurrent.html#global_io_executor-class_method" title="Concurrent.global_io_executor (method)">global_io_executor</a></span></span>
<span class='kw'>end</span>
<span class='kw'>end</span>
<span class='id identifier rubyid_pool'>pool</span> <span class='op'>=</span> <span class='const'><span class='object_link'><a href="../Concurrent.html" title="Concurrent (module)">Concurrent</a></span></span><span class='op'>::</span><span class='const'>Actor</span><span class='op'>::</span><span class='const'><span class='object_link'><a href="Actor/Utils.html" title="Concurrent::Actor::Utils (module)">Utils</a></span></span><span class='op'>::</span><span class='const'><span class='object_link'><a href="Actor/Utils/Pool.html" title="Concurrent::Actor::Utils::Pool (class)">Pool</a></span></span><span class='period'>.</span><span class='id identifier rubyid_spawn'><span class='object_link'><a href="Actor/AbstractContext.html#spawn-class_method" title="Concurrent::Actor::AbstractContext.spawn (method)">spawn</a></span></span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>pool</span><span class='tstring_end'>'</span></span><span class='comma'>,</span> <span class='int'>2</span><span class='rparen'>)</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_index'>index</span><span class='op'>|</span>
<span class='const'>IOWorker</span><span class='period'>.</span><span class='id identifier rubyid_spawn'>spawn</span><span class='lparen'>(</span><span class='label'>name:</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>worker-</span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_index'>index</span><span class='embexpr_end'>}</span><span class='tstring_end'>"</span></span><span class='rparen'>)</span>
<span class='kw'>end</span>
<span class='comment'># => #<Concurrent::Actor::Reference:0x7fbedba83378 /pool (Concurrent::Actor::Utils::Pool)>
</span>
<span class='id identifier rubyid_pool'>pool</span> <span class='op'><<</span> <span class='int'>1</span> <span class='op'><<</span> <span class='int'>2</span> <span class='op'><<</span> <span class='int'>3</span> <span class='op'><<</span> <span class='int'>4</span> <span class='op'><<</span> <span class='int'>5</span> <span class='op'><<</span> <span class='int'>6</span>
<span class='comment'># => #<Concurrent::Actor::Reference:0x7fbedba83378 /pool (Concurrent::Actor::Utils::Pool)>
</span>
<span class='comment'># prints two lines each second
</span><span class='comment'># /pool/worker-0 second:1414677666 message:1
</span><span class='comment'># /pool/worker-1 second:1414677666 message:2
</span><span class='comment'># /pool/worker-0 second:1414677667 message:3
</span><span class='comment'># /pool/worker-1 second:1414677667 message:4
</span><span class='comment'># /pool/worker-0 second:1414677668 message:5
</span><span class='comment'># /pool/worker-1 second:1414677668 message:6
</span>
<span class='id identifier rubyid_sleep'>sleep</span> <span class='int'>1</span> <span class='comment'># => 1</span></pre></p>
<h2>Dead letter routing</h2>
<p>see <span class='object_link'><a href="Actor/AbstractContext.html#dead_letter_routing-instance_method" title="Concurrent::Actor::AbstractContext#dead_letter_routing (method)">AbstractContext#dead_letter_routing</a></span> description:</p>
<blockquote>
<p>Defines an actor responsible for dead letters. Any rejected message send
with <span class='object_link'><a href="Actor/Reference.html#tell-instance_method" title="Concurrent::Actor::Reference#tell (method)">Reference#tell</a></span> is sent there, a message with future is considered
already monitored for failures. Default behaviour is to use
<span class='object_link'><a href="Actor/AbstractContext.html#dead_letter_routing-instance_method" title="Concurrent::Actor::AbstractContext#dead_letter_routing (method)">AbstractContext#dead_letter_routing</a></span> of the parent, so if no
<span class='object_link'><a href="Actor/AbstractContext.html#dead_letter_routing-instance_method" title="Concurrent::Actor::AbstractContext#dead_letter_routing (method)">AbstractContext#dead_letter_routing</a></span> method is overridden in
parent-chain the message ends up in <code>Actor.root.dead_letter_routing</code>
agent which will log warning.</p>
</blockquote>
<h2>FAQ</h2>
<h3>What happens if I try to supervise using a normal Context?</h3>
<p>Alleged supervisor will receive errors from its supervised actors. They'll have to be handled manually.</p>
<h3>How to change supervision strategy?</h3>
<p>Use option <code>behaviour_definition: Behaviour.restarting_behaviour_definition(:resume!)</code> or
<code>behaviour_definition: Behaviour.restarting_behaviour_definition(:reset!, :one_for_all)</code></p>
<h3>How to change behaviors?</h3>
<p>Any existing behavior can be subclassed </p>
<h3>How to implement custom restarting?</h3>
<p>By subclassing <span class='object_link'><a href="Actor/Behaviour/Pausing.html" title="Concurrent::Actor::Behaviour::Pausing (class)">Behaviour::Pausing</a></span> and overriding <span class='object_link'><a href="Actor/Behaviour/Pausing.html#restart!-instance_method" title="Concurrent::Actor::Behaviour::Pausing#restart! (method)">Behaviour::Pausing#restart!</a></span>. Implementing
<span class='object_link'><a href="Actor/AbstractContext.html#on_event-instance_method" title="Concurrent::Actor::AbstractContext#on_event (method)">AbstractContext#on_event</a></span> could be also considered.</p>
<p><em>We'll be happy to answer any other questions,
just <a href="https://github.com/ruby-concurrency/concurrent-ruby/issues/new">open an Issue</a> or find us on
<a href="https://gitter.im/ruby-concurrency/concurrent-ruby">https://gitter.im/ruby-concurrency/concurrent-ruby</a>.</em></p>
<h2>Speed</h2>
<p>Simple benchmark Actor vs Celluloid, the numbers are looking good
but you know how it is with benchmarks. Source code is in
<code>examples/actor/celluloid_benchmark.rb</code>. It sends numbers between x actors
and adding 1 until certain limit is reached.</p>
<p>Benchmark legend:</p>
<ul>
<li>mes. - number of messages send between the actors</li>
<li>act. - number of actors exchanging the messages</li>
<li>impl. - which gem is used</li>
</ul>
<h3>JRUBY</h3>
<pre class="code ruby"><code class="ruby">Rehearsal ---------------------------------------------------------
50000 2 concurrent 26.140000 0.610000 26.750000 ( 7.761000)
50000 2 celluloid 41.440000 5.270000 46.710000 ( 17.535000)
50000 500 concurrent 11.340000 0.180000 11.520000 ( 3.498000)
50000 500 celluloid 19.310000 10.680000 29.990000 ( 14.619000)
50000 1000 concurrent 10.640000 0.180000 10.820000 ( 3.563000)
50000 1000 celluloid 17.840000 19.850000 37.690000 ( 18.892000)
50000 1500 concurrent 14.120000 0.290000 14.410000 ( 4.618000)
50000 1500 celluloid 19.060000 28.920000 47.980000 ( 25.185000)
---------------------------------------------- total: 225.870000sec
mes. act. impl. user system total real
50000 2 concurrent 7.320000 0.530000 7.850000 ( 3.637000)
50000 2 celluloid 13.780000 4.730000 18.510000 ( 10.756000)
50000 500 concurrent 9.270000 0.140000 9.410000 ( 3.020000)
50000 500 celluloid 16.540000 10.920000 27.460000 ( 14.308000)
50000 1000 concurrent 9.970000 0.160000 10.130000 ( 3.445000)
50000 1000 celluloid 15.930000 20.840000 36.770000 ( 18.272000)
50000 1500 concurrent 11.580000 0.240000 11.820000 ( 3.723000)
50000 1500 celluloid 19.440000 29.060000 48.500000 ( 25.227000) (1)
</code></pre>
<h3>MRI 2.1.0</h3>
<pre class="code ruby"><code class="ruby">Rehearsal ---------------------------------------------------------
50000 2 concurrent 4.180000 0.080000 4.260000 ( 4.269435)
50000 2 celluloid 7.740000 3.100000 10.840000 ( 10.043875)
50000 500 concurrent 5.900000 1.310000 7.210000 ( 6.565067)
50000 500 celluloid 12.820000 5.810000 18.630000 ( 17.320765)
50000 1000 concurrent 6.080000 1.640000 7.720000 ( 6.931294)
50000 1000 celluloid 17.130000 8.320000 25.450000 ( 23.786146)
50000 1500 concurrent 6.940000 2.030000 8.970000 ( 7.927330)
50000 1500 celluloid 20.980000 12.040000 33.020000 ( 30.849578)
---------------------------------------------- total: 116.100000sec
mes. act. impl. user system total real
50000 2 concurrent 3.730000 0.100000 3.830000 ( 3.822688)
50000 2 celluloid 7.900000 2.910000 10.810000 ( 9.924014)
50000 500 concurrent 5.420000 1.230000 6.650000 ( 6.025579)
50000 500 celluloid 12.720000 5.540000 18.260000 ( 16.889517)
50000 1000 concurrent 5.420000 0.910000 6.330000 ( 5.896689)
50000 1000 celluloid 16.090000 8.040000 24.130000 ( 22.347102)
50000 1500 concurrent 5.580000 0.760000 6.340000 ( 6.038535)
50000 1500 celluloid 20.000000 11.680000 31.680000 ( 29.590774) (1)
</code></pre>
<p><em>Note (1):</em> Celluloid is using thread per actor so this bench is creating about 1500
native threads. Actor is using constant number of threads.</p>
</div>
</div>
<div class="tags">
</div><h2>Defined Under Namespace</h2>
<p class="children">
<strong class="modules">Modules:</strong> <span class='object_link'><a href="Actor/Behaviour.html" title="Concurrent::Actor::Behaviour (module)">Behaviour</a></span>, <span class='object_link'><a href="Actor/InternalDelegations.html" title="Concurrent::Actor::InternalDelegations (module)">InternalDelegations</a></span>, <span class='object_link'><a href="Actor/PublicDelegations.html" title="Concurrent::Actor::PublicDelegations (module)">PublicDelegations</a></span>, <span class='object_link'><a href="Actor/TypeCheck.html" title="Concurrent::Actor::TypeCheck (module)">TypeCheck</a></span>, <span class='object_link'><a href="Actor/Utils.html" title="Concurrent::Actor::Utils (module)">Utils</a></span>
<strong class="classes">Classes:</strong> <span class='object_link'><a href="Actor/AbstractContext.html" title="Concurrent::Actor::AbstractContext (class)">AbstractContext</a></span>, <span class='object_link'><a href="Actor/ActorTerminated.html" title="Concurrent::Actor::ActorTerminated (class)">ActorTerminated</a></span>, <span class='object_link'><a href="Actor/Context.html" title="Concurrent::Actor::Context (class)">Context</a></span>, <span class='object_link'><a href="Actor/Core.html" title="Concurrent::Actor::Core (class)">Core</a></span>, <span class='object_link'><a href="Actor/DefaultDeadLetterHandler.html" title="Concurrent::Actor::DefaultDeadLetterHandler (class)">DefaultDeadLetterHandler</a></span>, <span class='object_link'><a href="Actor/Envelope.html" title="Concurrent::Actor::Envelope (class)">Envelope</a></span>, <span class='object_link'><a href="Actor/Reference.html" title="Concurrent::Actor::Reference (class)">Reference</a></span>, <span class='object_link'><a href="Actor/RestartingContext.html" title="Concurrent::Actor::RestartingContext (class)">RestartingContext</a></span>, <span class='object_link'><a href="Actor/Root.html" title="Concurrent::Actor::Root (class)">Root</a></span>, <span class='object_link'><a href="Actor/UnknownMessage.html" title="Concurrent::Actor::UnknownMessage (class)">UnknownMessage</a></span>
</p>
<h2>
Constant Summary
<small><a href="#" class="constants_summary_toggle">collapse</a></small>
</h2>
<dl class="constants">
<dt id="Error-constant" class="">Error =
<div class="docstring">
<div class="discussion">
</div>
</div>
<div class="tags">
</div>
</dt>
<dd><pre class="code"><span class='const'>Class</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span><span class='lparen'>(</span><span class='const'>StandardError</span><span class='rparen'>)</span></pre></dd>
</dl>
<h2>
Class Method Summary
<small><a href="#" class="summary_toggle">collapse</a></small>
</h2>
<ul class="summary">
<li class="public ">
<span class="summary_signature">
<a href="#current-class_method" title="current (class method)">.<strong>current</strong> ⇒ Reference<sup>?</sup> </a>
</span>
<span class="summary_desc"><div class='inline'><p>Current executing actor if any.</p>
</div></span>
</li>
<li class="public ">
<span class="summary_signature">
<a href="#root-class_method" title="root (class method)">.<strong>root</strong> ⇒ undocumented </a>
</span>
<span class="summary_desc"><div class='inline'><p>A root actor, a default parent of all actors spawned outside an actor.</p>
</div></span>
</li>
<li class="public ">
<span class="summary_signature">
<a href="#spawn-class_method" title="spawn (class method)">.<strong>spawn</strong>(*args, &block) ⇒ Reference </a>
</span>
<span class="summary_desc"><div class='inline'><p>Spawns a new actor.</p>
</div></span>
</li>
<li class="public ">
<span class="summary_signature">
<a href="#spawn!-class_method" title="spawn! (class method)">.<strong>spawn!</strong>(*args, &block) ⇒ undocumented </a>
</span>
<span class="summary_desc"><div class='inline'><p>as <span class='object_link'><a href="#spawn-class_method" title="Concurrent::Actor.spawn (method)">Actor.spawn</a></span> but it'll block until actor is initialized or it'll raise exception on error.</p>
</div></span>
</li>
<li class="public ">
<span class="summary_signature">
<a href="#to_spawn_options-class_method" title="to_spawn_options (class method)">.<strong>to_spawn_options</strong>(*args) ⇒ undocumented </a>
</span>
<span class="summary_desc"><div class='inline'></div></span>
</li>
</ul>
<div id="class_method_details" class="method_details_list">
<h2>Class Method Details</h2>
<div class="method_details first">
<h3 class="signature first" id="current-class_method">
.<strong>current</strong> ⇒ <tt><span class='object_link'><a href="Actor/Reference.html" title="Concurrent::Actor::Reference (class)">Reference</a></span></tt><sup>?</sup>
</h3><div class="docstring">
<div class="discussion">
<p>Returns current executing actor if any</p>
</div>
</div>
<div class="tags">
<p class="tag_title">Returns:</p>
<ul class="return">
<li>
<span class='type'>(<tt><span class='object_link'><a href="Actor/Reference.html" title="Concurrent::Actor::Reference (class)">Reference</a></span></tt>, <tt>nil</tt>)</span>
—
<div class='inline'><p>current executing actor if any</p>
</div>
</li>
</ul>
</div><table class="source_code">
<tr>
<td>
<pre class="lines">
33
34
35</pre>
</td>
<td>
<pre class="code"><span class="info file"># File 'lib-edge/concurrent/actor.rb', line 33</span>
<span class='kw'>def</span> <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_current'>current</span>
<span class='const'>Thread</span><span class='period'>.</span><span class='id identifier rubyid_current'>current</span><span class='lbracket'>[</span><span class='symbol'>:__current_actor__</span><span class='rbracket'>]</span>
<span class='kw'>end</span></pre>
</td>
</tr>
</table>
</div>
<div class="method_details ">
<h3 class="signature " id="root-class_method">
.<strong>root</strong> ⇒ <tt>undocumented</tt>
</h3><div class="docstring">
<div class="discussion">
<p>A root actor, a default parent of all actors spawned outside an actor</p>
</div>
</div>
<div class="tags">
</div><table class="source_code">
<tr>
<td>
<pre class="lines">
48
49
50</pre>
</td>
<td>
<pre class="code"><span class="info file"># File 'lib-edge/concurrent/actor.rb', line 48</span>
<span class='kw'>def</span> <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_root'><span class='object_link'><a href="../top-level-namespace.html" title="Top Level Namespace (root)">root</a></span></span>
<span class='ivar'>@root</span><span class='period'>.</span><span class='id identifier rubyid_value!'>value!</span>
<span class='kw'>end</span></pre>
</td>
</tr>
</table>
</div>
<div class="method_details ">
<h3 class="signature " id="spawn-class_method">
.<strong>spawn</strong>(*args, &block) ⇒ <tt><span class='object_link'><a href="Actor/Reference.html" title="Concurrent::Actor::Reference (class)">Reference</a></span></tt>
</h3><div class="docstring">
<div class="discussion">
<p>Spawns a new actor. <span class='object_link'><a href="Actor/AbstractContext.html#spawn-class_method" title="Concurrent::Actor::AbstractContext.spawn (method)">Concurrent::Actor::AbstractContext.spawn</a></span> allows to omit class parameter.
To see the list of available options see <span class='object_link'><a href="Actor/Core.html#initialize-instance_method" title="Concurrent::Actor::Core#initialize (method)">Concurrent::Actor::Core#initialize</a></span></p>
</div>
</div>
<div class="tags">
<div class="examples">
<p class="tag_title">Examples:</p>
<p class="example_title"><div class='inline'><p>by class and name</p>
</div></p>
<pre class="example code"><code><span class='const'><span class='object_link'><a href="" title="Concurrent::Actor (module)">Actor</a></span></span><span class='period'>.</span><span class='id identifier rubyid_spawn'>spawn</span><span class='lparen'>(</span><span class='const'>AdHoc</span><span class='comma'>,</span> <span class='symbol'>:ping1</span><span class='rparen'>)</span> <span class='lbrace'>{</span> <span class='tlambda'>-></span> <span class='id identifier rubyid_message'>message</span> <span class='tlambeg'>{</span> <span class='id identifier rubyid_message'>message</span> <span class='rbrace'>}</span> <span class='rbrace'>}</span></code></pre>
<p class="example_title"><div class='inline'><p>by option hash</p>
</div></p>
<pre class="example code"><code><span class='id identifier rubyid_inc2'>inc2</span> <span class='op'>=</span> <span class='const'><span class='object_link'><a href="" title="Concurrent::Actor (module)">Actor</a></span></span><span class='period'>.</span><span class='id identifier rubyid_spawn'>spawn</span><span class='lparen'>(</span><span class='label'>class:</span> <span class='const'>AdHoc</span><span class='comma'>,</span>
<span class='label'>name:</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>increment by 2</span><span class='tstring_end'>'</span></span><span class='comma'>,</span>
<span class='label'>args:</span> <span class='lbracket'>[</span><span class='int'>2</span><span class='rbracket'>]</span><span class='comma'>,</span>
<span class='label'>executor:</span> <span class='const'><span class='object_link'><a href="../Concurrent.html" title="Concurrent (module)">Concurrent</a></span></span><span class='period'>.</span><span class='id identifier rubyid_global_io_executor'><span class='object_link'><a href="../Concurrent.html#global_io_executor-class_method" title="Concurrent.global_io_executor (method)">global_io_executor</a></span></span><span class='rparen'>)</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_increment_by'>increment_by</span><span class='op'>|</span>
<span class='id identifier rubyid_lambda'>lambda</span> <span class='lbrace'>{</span> <span class='op'>|</span><span class='id identifier rubyid_number'>number</span><span class='op'>|</span> <span class='id identifier rubyid_number'>number</span> <span class='op'>+</span> <span class='id identifier rubyid_increment_by'>increment_by</span> <span class='rbrace'>}</span>
<span class='kw'>end</span>
<span class='id identifier rubyid_inc2'>inc2</span><span class='period'>.</span><span class='id identifier rubyid_ask!'>ask!</span><span class='lparen'>(</span><span class='int'>2</span><span class='rparen'>)</span> <span class='comment'># => 4</span></code></pre>
</div>