celluloid 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,5 +1,6 @@
1
1
  Celluloid
2
2
  =========
3
+ [![Build Status](http://travis-ci.org/tarcieri/celluloid.png)](http://travis-ci.org/tarcieri/celluloid)
3
4
 
4
5
  > "I thought of objects being like biological cells and/or individual
5
6
  > computers on a network, only able to communicate with messages"
@@ -27,10 +28,10 @@ Celluloid works on Rubinius in either 1.8 or 1.9 mode.
27
28
  Usage
28
29
  -----
29
30
 
30
- To use Celluloid, define a normal Ruby class that includes Celluloid::Actor
31
+ To use Celluloid, define a normal Ruby class that includes Celluloid:
31
32
 
32
33
  class Sheen
33
- include Celluloid::Actor
34
+ include Celluloid
34
35
 
35
36
  def initialize(name)
36
37
  @name = name
@@ -45,10 +46,10 @@ To use Celluloid, define a normal Ruby class that includes Celluloid::Actor
45
46
  end
46
47
  end
47
48
 
48
- If you call Sheen.new, you'll wind up with a plain old Ruby object. To
49
- create a concurrent object instead of a regular one, use Sheen.spawn:
49
+ Now when you create new instances of this class, they're actually concurrent
50
+ objects, each running in their own thread:
50
51
 
51
- >> charlie = Sheen.spawn "Charlie Sheen"
52
+ >> charlie = Sheen.new "Charlie Sheen"
52
53
  => #<Celluloid::Actor(Sheen:0x00000100a312d0) @name="Charlie Sheen">
53
54
  >> charlie.set_status "winning!"
54
55
  => "winning!"
@@ -59,10 +60,9 @@ create a concurrent object instead of a regular one, use Sheen.spawn:
59
60
  >> charlie.report
60
61
  => "Charlie Sheen is asynchronously winning!"
61
62
 
62
- Calling spawn creates a concurrent object running inside its own thread. You
63
- can call methods on this concurrent object just like you would any other Ruby
64
- object. The Sheen#set_status method works exactly like you'd expect, returning
65
- the last expression evaluated.
63
+ You can call methods on this concurrent object just like you would any other
64
+ Ruby object. The Sheen#set_status method works exactly like you'd expect,
65
+ returning the last expression evaluated.
66
66
 
67
67
  However, Celluloid's secret sauce kicks in when you call banged predicate
68
68
  methods (i.e. methods ending in !). Even though the Sheen class has no
@@ -89,6 +89,38 @@ crashed object will not raise an error.
89
89
  However, you can still handle errors created by asynchronous calls using
90
90
  two features of Celluloid called _supervisors_ and _linking_.
91
91
 
92
+ Futures
93
+ -------
94
+
95
+ Futures allow you to request a computation and get the result later. There are
96
+ two types of futures supported by Celluloid: method futures and block futures.
97
+ Method futures work by invoking the _future_ method on an actor. This method
98
+ is analogous to the typical _send_ method in that it takes a method name,
99
+ followed by an arbitrary number of arguments, and a block. Let's invoke the
100
+ report method from the charlie object used in the above example using a future:
101
+
102
+ >> future = charlie.future :report
103
+ => #<Celluloid::Future:0x000001009759b8>
104
+ >> future.value
105
+ => "Charlie Sheen is winning!"
106
+
107
+ The call to charlie.future immediately returns a Celluloid::Future object,
108
+ regardless of how long it takes to execute the "report" method. To obtain
109
+ the result of the call to "report", we call the _value_ method of the
110
+ future object. This call will block until the method call is available.
111
+
112
+ Futures also allow you to background the computation of any block:
113
+
114
+ >> future = Celluloid::Future.new { 2 + 2 }
115
+ => #<Celluloid::Future:0x000001008425f0>
116
+ >> future.value
117
+ => 4
118
+
119
+ One thing to be aware of when using futures: always make sure to obtain the
120
+ value of any future you make. Futures create a thread in the background which
121
+ will continue to run until the future's value is obtained. Failing to obtain
122
+ the value of futures you create will leak threads.
123
+
92
124
  Supervisors
93
125
  -----------
94
126
 
@@ -106,7 +138,7 @@ class from the example above:
106
138
 
107
139
  This created a new Celluloid::Supervisor actor, and also created a new Sheen
108
140
  actor, giving its initialize method the argument "Charlie Sheen". The
109
- _supervise_ method has the same method signature as _spawn_. However, rather
141
+ _supervise_ method has the same method signature as _new_. However, rather
110
142
  than returning the newly created actor, _supervise_ returns the supervisor.
111
143
  To retrieve the actor that the supervisor is currently using, use the
112
144
  Celluloid::Supervisor#actor method:
@@ -128,7 +160,7 @@ In this case, the supervisor will ensure that an actor of the Sheen class,
128
160
  created using the given arguments, is aways available by calling
129
161
  Celluloid::Actor[:charlie]. The first argument to supervise_as is the name
130
162
  you'd like the newly created actor to be registered under. The remaining
131
- arguments are passed to initialize just like you called _new_ or _spawn_.
163
+ arguments are passed to initialize just like you called _new_.
132
164
 
133
165
  See the "Registry" section below for more information on the actor registry
134
166
 
@@ -139,7 +171,7 @@ Whenever any unhandled exceptions occur in any of the methods of an actor,
139
171
  that actor crashes and dies. Let's start with an example:
140
172
 
141
173
  class JamesDean
142
- include Celluloid::Actor
174
+ include Celluloid
143
175
  class CarInMyLaneError < StandardError; end
144
176
 
145
177
  def drive_little_bastard
@@ -149,7 +181,7 @@ that actor crashes and dies. Let's start with an example:
149
181
 
150
182
  Now, let's have James drive Little Bastard and see what happens:
151
183
 
152
- >> james = JamesDean.spawn
184
+ >> james = JamesDean.new
153
185
  => #<Celluloid::Actor(JamesDean:0x1068)>
154
186
  >> james.drive_little_bastard!
155
187
  => nil
@@ -166,7 +198,7 @@ trap_exit method to be notified of them. Let's look at how a hypothetical
166
198
  Elizabeth Taylor object could be notified that James Dean has crashed:
167
199
 
168
200
  class ElizabethTaylor
169
- include Celluloid::Actor
201
+ include Celluloid
170
202
  trap_exit :actor_died
171
203
 
172
204
  def actor_died(actor, reason)
@@ -178,9 +210,9 @@ We've now used the trap_exit method to configure a callback which is invoked
178
210
  whenever any linked actors crashed. Now we need to link Elizabeth to James so
179
211
  James' crash notifications get sent to her:
180
212
 
181
- >> james = JamesDean.spawn
213
+ >> james = JamesDean.new
182
214
  => #<Celluloid::Actor(JamesDean:0x11b8)>
183
- >> elizabeth = ElizabethTaylor.spawn
215
+ >> elizabeth = ElizabethTaylor.new
184
216
  => #<Celluloid::Actor(ElizabethTaylor:0x11f0)>
185
217
  >> elizabeth.link james
186
218
  => #<Celluloid::Actor(JamesDean:0x11b8)>
@@ -197,7 +229,7 @@ if she weren't trapping exits? Let's break James apart into two separate
197
229
  objects, one for James himself and one for Little Bastard, his car:
198
230
 
199
231
  class PorscheSpider
200
- include Celluloid::Actor
232
+ include Celluloid
201
233
  class CarInMyLaneError < StandardError; end
202
234
 
203
235
  def drive_on_route_466
@@ -206,10 +238,10 @@ objects, one for James himself and one for Little Bastard, his car:
206
238
  end
207
239
 
208
240
  class JamesDean
209
- include Celluloid::Actor
241
+ include Celluloid
210
242
 
211
243
  def initialize
212
- @little_bastard = PorscheSpider.spawn_link
244
+ @little_bastard = PorscheSpider.new_link
213
245
  end
214
246
 
215
247
  def drive_little_bastard
@@ -218,17 +250,17 @@ objects, one for James himself and one for Little Bastard, his car:
218
250
  end
219
251
 
220
252
  If you take a look in JamesDean#initialize, you'll notice that to create an
221
- instance of PorcheSpider, James is calling the spawn_link method.
253
+ instance of PorcheSpider, James is calling the new_link method.
222
254
 
223
- This method works similarly to _spawn_, except it combines _spawn_ and _link_
255
+ This method works similarly to _new_, except it combines _new_ and _link_
224
256
  into a single call.
225
257
 
226
258
  Now what happens if we repeat the same scenario with Elizabeth Taylor watching
227
259
  for James Dean's crash?
228
260
 
229
- >> james = JamesDean.spawn
261
+ >> james = JamesDean.new
230
262
  => #<Celluloid::Actor(JamesDean:0x1108) @little_bastard=#<Celluloid::Actor(PorscheSpider:0x10ec)>>
231
- >> elizabeth = ElizabethTaylor.spawn
263
+ >> elizabeth = ElizabethTaylor.new
232
264
  => #<Celluloid::Actor(ElizabethTaylor:0x1144)>
233
265
  >> elizabeth.link james
234
266
  => #<Celluloid::Actor(JamesDean:0x1108) @little_bastard=#<Celluloid::Actor(PorscheSpider:0x10ec)>>
@@ -250,7 +282,7 @@ This allows you to factor your problem into several actors. If an error occurs
250
282
  in any of them, it will kill off all actors used in a particular system. In
251
283
  general, you'll probably want to have a supervisor start a single actor which
252
284
  is in charge of a particular part of your system, and have that actor
253
- spawn_link to other actors which are part of the same system. If any error
285
+ new_link to other actors which are part of the same system. If any error
254
286
  occurs in any of these actors, all of them will be killed off and the entire
255
287
  subsystem will be restarted by the supervisor in a clean state.
256
288
 
@@ -263,7 +295,7 @@ Registry
263
295
  Celluloid lets you register actors so you can refer to them symbolically.
264
296
  You can register Actors using Celluloid::Actor[]:
265
297
 
266
- >> james = JamesDean.spawn
298
+ >> james = JamesDean.new
267
299
  => #<Celluloid::Actor(JamesDean:0x80c27ce0)>
268
300
  >> Celluloid::Actor[:james] = james
269
301
  => #<Celluloid::Actor(JamesDean:0x80c27ce0)>
@@ -280,6 +312,41 @@ working, freshly-restarted version.
280
312
  The main use of the registry is for interfacing with actors that are
281
313
  automatically restarted by supervisors when they crash.
282
314
 
315
+ Signaling
316
+ ---------
317
+
318
+ Signaling is an advanced technique similar to condition variables in typical
319
+ multithreaded programming. One method within a concurrent object can suspend
320
+ itself waiting for a particular event, allowing other methods to run. Another
321
+ method can then signal all methods waiting for a particular event, and even
322
+ send them a value in the process:
323
+
324
+ class SignalingExample
325
+ include Celluloid
326
+ attr_reader :signaled
327
+
328
+ def initialize
329
+ @signaled = false
330
+ end
331
+
332
+ def wait_for_signal
333
+ value = wait :ponycopter
334
+ @signaled = true
335
+ value
336
+ end
337
+
338
+ def send_signal(value)
339
+ signal :ponycopter, value
340
+ end
341
+ end
342
+
343
+ The wait_for_signal method in turn calls a method called "wait". Wait suspends
344
+ the running method until another method of the same object calls the "signal"
345
+ method with the same label.
346
+
347
+ The send_signal method of this class does just that, signaling "ponycopter"
348
+ with the given value. This value is returned from the original wait call.
349
+
283
350
  Logging
284
351
  -------
285
352
 
@@ -300,4 +367,4 @@ Contributing to Celluloid
300
367
  Copyright
301
368
  ---------
302
369
 
303
- Copyright (c) 2011 Tony Arcieri. See LICENSE.txt for further details.
370
+ Copyright (c) 2011 Tony Arcieri. See LICENSE.txt for further details.
@@ -1,5 +1,22 @@
1
1
  require 'logger'
2
2
 
3
+ module Celluloid
4
+ @@logger_lock = Mutex.new
5
+ @@logger = Logger.new STDERR
6
+
7
+ def self.logger
8
+ @@logger_lock.synchronize { @@logger }
9
+ end
10
+
11
+ def self.logger=(logger)
12
+ @@logger_lock.synchronize { @@logger = logger }
13
+ end
14
+
15
+ def self.included(klass)
16
+ klass.send :include, Actor
17
+ end
18
+ end
19
+
3
20
  require 'celluloid/version'
4
21
  require 'celluloid/actor'
5
22
  require 'celluloid/actor_proxy'
@@ -10,19 +27,7 @@ require 'celluloid/linking'
10
27
  require 'celluloid/mailbox'
11
28
  require 'celluloid/registry'
12
29
  require 'celluloid/responses'
30
+ require 'celluloid/signals'
13
31
  require 'celluloid/supervisor'
14
32
 
15
33
  require 'celluloid/future'
16
-
17
- module Celluloid
18
- @@logger_lock = Mutex.new
19
- @@logger = Logger.new STDERR
20
-
21
- def self.logger
22
- @@logger_lock.synchronize { @@logger }
23
- end
24
-
25
- def self.logger=(logger)
26
- @@logger_lock.synchronize { @@logger = logger }
27
- end
28
- end
@@ -1,5 +1,5 @@
1
1
  !RBIX
2
- 3578385345186687227
2
+ 3246536075095810518
3
3
  18
4
4
  M
5
5
  1
@@ -9,7 +9,7 @@ x
9
9
  10
10
10
  __script__
11
11
  i
12
- 145
12
+ 154
13
13
  5
14
14
  7
15
15
  0
@@ -19,9 +19,35 @@ i
19
19
  1
20
20
  1
21
21
  15
22
- 5
22
+ 99
23
23
  7
24
24
  2
25
+ 65
26
+ 49
27
+ 3
28
+ 2
29
+ 13
30
+ 99
31
+ 12
32
+ 7
33
+ 4
34
+ 12
35
+ 7
36
+ 5
37
+ 12
38
+ 65
39
+ 12
40
+ 49
41
+ 6
42
+ 4
43
+ 15
44
+ 49
45
+ 4
46
+ 0
47
+ 15
48
+ 5
49
+ 7
50
+ 7
25
51
  64
26
52
  47
27
53
  49
@@ -30,7 +56,7 @@ i
30
56
  15
31
57
  5
32
58
  7
33
- 3
59
+ 8
34
60
  64
35
61
  47
36
62
  49
@@ -39,7 +65,7 @@ i
39
65
  15
40
66
  5
41
67
  7
42
- 4
68
+ 9
43
69
  64
44
70
  47
45
71
  49
@@ -48,7 +74,7 @@ i
48
74
  15
49
75
  5
50
76
  7
51
- 5
77
+ 10
52
78
  64
53
79
  47
54
80
  49
@@ -57,7 +83,7 @@ i
57
83
  15
58
84
  5
59
85
  7
60
- 6
86
+ 11
61
87
  64
62
88
  47
63
89
  49
@@ -66,7 +92,7 @@ i
66
92
  15
67
93
  5
68
94
  7
69
- 7
95
+ 12
70
96
  64
71
97
  47
72
98
  49
@@ -75,7 +101,7 @@ i
75
101
  15
76
102
  5
77
103
  7
78
- 8
104
+ 13
79
105
  64
80
106
  47
81
107
  49
@@ -84,7 +110,7 @@ i
84
110
  15
85
111
  5
86
112
  7
87
- 9
113
+ 14
88
114
  64
89
115
  47
90
116
  49
@@ -93,7 +119,7 @@ i
93
119
  15
94
120
  5
95
121
  7
96
- 10
122
+ 15
97
123
  64
98
124
  47
99
125
  49
@@ -102,7 +128,7 @@ i
102
128
  15
103
129
  5
104
130
  7
105
- 11
131
+ 16
106
132
  64
107
133
  47
108
134
  49
@@ -111,7 +137,7 @@ i
111
137
  15
112
138
  5
113
139
  7
114
- 12
140
+ 17
115
141
  64
116
142
  47
117
143
  49
@@ -120,38 +146,21 @@ i
120
146
  15
121
147
  5
122
148
  7
123
- 13
149
+ 18
124
150
  64
125
151
  47
126
152
  49
127
153
  1
128
154
  1
129
155
  15
130
- 99
131
- 7
132
- 14
133
- 65
134
- 49
135
- 15
136
- 2
137
- 13
138
- 99
139
- 12
140
- 7
141
- 16
142
- 12
156
+ 5
143
157
  7
144
- 17
145
- 12
146
- 65
147
- 12
148
- 49
149
- 18
150
- 4
151
- 15
158
+ 19
159
+ 64
160
+ 47
152
161
  49
153
- 16
154
- 0
162
+ 1
163
+ 1
155
164
  15
156
165
  2
157
166
  11
@@ -167,49 +176,13 @@ I
167
176
  0
168
177
  n
169
178
  p
170
- 19
179
+ 20
171
180
  s
172
181
  6
173
182
  logger
174
183
  x
175
184
  7
176
185
  require
177
- s
178
- 17
179
- celluloid/version
180
- s
181
- 15
182
- celluloid/actor
183
- s
184
- 21
185
- celluloid/actor_proxy
186
- s
187
- 15
188
- celluloid/calls
189
- s
190
- 18
191
- celluloid/core_ext
192
- s
193
- 16
194
- celluloid/events
195
- s
196
- 17
197
- celluloid/linking
198
- s
199
- 17
200
- celluloid/mailbox
201
- s
202
- 18
203
- celluloid/registry
204
- s
205
- 19
206
- celluloid/responses
207
- s
208
- 20
209
- celluloid/supervisor
210
- s
211
- 16
212
- celluloid/future
213
186
  x
214
187
  9
215
188
  Celluloid
@@ -227,7 +200,7 @@ x
227
200
  9
228
201
  Celluloid
229
202
  i
230
- 92
203
+ 103
231
204
  5
232
205
  66
233
206
  5
@@ -319,6 +292,17 @@ i
319
292
  49
320
293
  15
321
294
  4
295
+ 15
296
+ 99
297
+ 7
298
+ 18
299
+ 7
300
+ 19
301
+ 65
302
+ 5
303
+ 49
304
+ 15
305
+ 4
322
306
  11
323
307
  I
324
308
  5
@@ -332,7 +316,7 @@ I
332
316
  0
333
317
  n
334
318
  p
335
- 18
319
+ 20
336
320
  x
337
321
  13
338
322
  @@logger_lock
@@ -453,7 +437,7 @@ p
453
437
  I
454
438
  0
455
439
  I
456
- 16
440
+ 8
457
441
  I
458
442
  7
459
443
  x
@@ -469,11 +453,11 @@ p
469
453
  I
470
454
  -1
471
455
  I
472
- 15
456
+ 7
473
457
  I
474
458
  0
475
459
  I
476
- 16
460
+ 8
477
461
  I
478
462
  c
479
463
  x
@@ -576,7 +560,7 @@ p
576
560
  I
577
561
  0
578
562
  I
579
- 1a
563
+ c
580
564
  I
581
565
  a
582
566
  x
@@ -592,11 +576,11 @@ p
592
576
  I
593
577
  -1
594
578
  I
595
- 19
579
+ b
596
580
  I
597
581
  0
598
582
  I
599
- 1a
583
+ c
600
584
  I
601
585
  c
602
586
  x
@@ -607,26 +591,96 @@ p
607
591
  x
608
592
  6
609
593
  logger
594
+ x
595
+ 8
596
+ included
597
+ M
598
+ 1
599
+ n
600
+ n
601
+ x
602
+ 8
603
+ included
604
+ i
605
+ 11
606
+ 20
607
+ 0
608
+ 7
609
+ 0
610
+ 45
611
+ 1
612
+ 2
613
+ 49
614
+ 3
615
+ 2
616
+ 11
617
+ I
618
+ 4
619
+ I
620
+ 1
621
+ I
622
+ 1
623
+ I
624
+ 0
625
+ I
626
+ 1
627
+ n
610
628
  p
611
- 9
629
+ 4
630
+ x
631
+ 7
632
+ include
633
+ x
634
+ 5
635
+ Actor
636
+ n
637
+ x
638
+ 4
639
+ send
640
+ p
641
+ 5
642
+ I
643
+ -1
644
+ I
645
+ f
646
+ I
647
+ 0
648
+ I
649
+ 10
650
+ I
651
+ b
652
+ x
653
+ 42
654
+ /Users/tony/src/celluloid/lib/celluloid.rb
655
+ p
656
+ 1
657
+ x
658
+ 5
659
+ klass
660
+ p
661
+ 11
612
662
  I
613
663
  2
614
664
  I
615
- 12
665
+ 4
616
666
  I
617
667
  21
618
668
  I
619
- 13
669
+ 5
620
670
  I
621
671
  46
622
672
  I
623
- 15
673
+ 7
624
674
  I
625
675
  51
626
676
  I
627
- 19
677
+ b
628
678
  I
629
679
  5c
680
+ I
681
+ f
682
+ I
683
+ 67
630
684
  x
631
685
  42
632
686
  /Users/tony/src/celluloid/lib/celluloid.rb
@@ -635,8 +689,47 @@ p
635
689
  x
636
690
  13
637
691
  attach_method
692
+ s
693
+ 17
694
+ celluloid/version
695
+ s
696
+ 15
697
+ celluloid/actor
698
+ s
699
+ 21
700
+ celluloid/actor_proxy
701
+ s
702
+ 15
703
+ celluloid/calls
704
+ s
705
+ 18
706
+ celluloid/core_ext
707
+ s
708
+ 16
709
+ celluloid/events
710
+ s
711
+ 17
712
+ celluloid/linking
713
+ s
714
+ 17
715
+ celluloid/mailbox
716
+ s
717
+ 18
718
+ celluloid/registry
719
+ s
720
+ 19
721
+ celluloid/responses
722
+ s
723
+ 17
724
+ celluloid/signals
725
+ s
726
+ 20
727
+ celluloid/supervisor
728
+ s
729
+ 16
730
+ celluloid/future
638
731
  p
639
- 29
732
+ 31
640
733
  I
641
734
  0
642
735
  I
@@ -646,55 +739,59 @@ I
646
739
  I
647
740
  3
648
741
  I
649
- 12
742
+ 23
650
743
  I
651
- 4
744
+ 14
652
745
  I
653
- 1b
746
+ 2c
654
747
  I
655
- 5
748
+ 15
656
749
  I
657
- 24
750
+ 35
658
751
  I
659
- 6
752
+ 16
660
753
  I
661
- 2d
754
+ 3e
662
755
  I
663
- 7
756
+ 17
664
757
  I
665
- 36
758
+ 47
666
759
  I
667
- 8
760
+ 18
668
761
  I
669
- 3f
762
+ 50
670
763
  I
671
- 9
764
+ 19
672
765
  I
673
- 48
766
+ 59
674
767
  I
675
- a
768
+ 1a
676
769
  I
677
- 51
770
+ 62
678
771
  I
679
- b
772
+ 1b
680
773
  I
681
- 5a
774
+ 6b
682
775
  I
683
- c
776
+ 1c
684
777
  I
685
- 63
778
+ 74
686
779
  I
687
- d
780
+ 1d
688
781
  I
689
- 6c
782
+ 7d
690
783
  I
691
- f
784
+ 1e
692
785
  I
693
- 75
786
+ 86
694
787
  I
695
- 11
788
+ 1f
789
+ I
790
+ 8f
791
+ I
792
+ 21
696
793
  I
697
- 91
794
+ 9a
698
795
  x
699
796
  42
700
797
  /Users/tony/src/celluloid/lib/celluloid.rb