eventmachine 0.12.10-x86-mswin32-60 → 1.0.0.beta.2-x86-mswin32-60

Sign up to get free protection for your applications and to get access to all the features.
Files changed (150) hide show
  1. data/.gitignore +2 -0
  2. data/Gemfile +1 -0
  3. data/README +80 -81
  4. data/Rakefile +7 -370
  5. data/docs/COPYING +60 -60
  6. data/docs/ChangeLog +211 -211
  7. data/docs/DEFERRABLES +246 -133
  8. data/docs/EPOLL +141 -141
  9. data/docs/GNU +281 -281
  10. data/docs/INSTALL +13 -13
  11. data/docs/KEYBOARD +42 -38
  12. data/docs/LEGAL +25 -25
  13. data/docs/LIGHTWEIGHT_CONCURRENCY +130 -70
  14. data/docs/PURE_RUBY +75 -75
  15. data/docs/RELEASE_NOTES +94 -94
  16. data/docs/SMTP +4 -2
  17. data/docs/SPAWNED_PROCESSES +148 -89
  18. data/docs/TODO +8 -8
  19. data/eventmachine.gemspec +19 -26
  20. data/examples/ex_channel.rb +42 -42
  21. data/examples/ex_queue.rb +2 -2
  22. data/examples/ex_tick_loop_array.rb +15 -0
  23. data/examples/ex_tick_loop_counter.rb +32 -0
  24. data/examples/helper.rb +1 -1
  25. data/ext/binder.cpp +0 -1
  26. data/ext/cmain.cpp +36 -25
  27. data/ext/ed.cpp +104 -113
  28. data/ext/ed.h +24 -30
  29. data/ext/em.cpp +349 -283
  30. data/ext/em.h +25 -29
  31. data/ext/eventmachine.h +5 -4
  32. data/ext/extconf.rb +58 -49
  33. data/ext/fastfilereader/extconf.rb +5 -3
  34. data/ext/fastfilereader/mapper.cpp +214 -214
  35. data/ext/fastfilereader/mapper.h +59 -59
  36. data/ext/fastfilereader/rubymain.cpp +127 -127
  37. data/ext/kb.cpp +1 -3
  38. data/ext/page.cpp +107 -107
  39. data/ext/page.h +51 -51
  40. data/ext/pipe.cpp +9 -11
  41. data/ext/project.h +12 -8
  42. data/ext/rubymain.cpp +138 -104
  43. data/java/.classpath +8 -8
  44. data/java/.project +17 -17
  45. data/java/src/com/rubyeventmachine/EmReactor.java +1 -0
  46. data/java/src/com/rubyeventmachine/EmReactorException.java +40 -40
  47. data/lib/em/buftok.rb +138 -138
  48. data/lib/em/callback.rb +25 -25
  49. data/lib/em/channel.rb +1 -1
  50. data/lib/em/connection.rb +6 -1
  51. data/lib/em/deferrable.rb +16 -2
  52. data/lib/em/file_watch.rb +53 -53
  53. data/lib/em/future.rb +61 -61
  54. data/lib/em/iterator.rb +270 -0
  55. data/lib/em/messages.rb +66 -66
  56. data/lib/em/process_watch.rb +43 -43
  57. data/lib/em/protocols.rb +1 -1
  58. data/lib/em/protocols/header_and_content.rb +138 -138
  59. data/lib/em/protocols/httpclient.rb +267 -262
  60. data/lib/em/protocols/line_protocol.rb +28 -0
  61. data/lib/em/protocols/memcache.rb +322 -322
  62. data/lib/em/protocols/postgres3.rb +247 -247
  63. data/lib/em/protocols/saslauth.rb +175 -175
  64. data/lib/em/protocols/smtpserver.rb +640 -547
  65. data/lib/em/protocols/stomp.rb +200 -200
  66. data/lib/em/protocols/tcptest.rb +52 -52
  67. data/lib/{pr_eventmachine.rb → em/pure_ruby.rb} +1013 -1022
  68. data/lib/em/queue.rb +1 -0
  69. data/lib/em/spawnable.rb +85 -85
  70. data/lib/em/streamer.rb +130 -130
  71. data/lib/em/tick_loop.rb +85 -0
  72. data/lib/em/timers.rb +2 -1
  73. data/lib/em/version.rb +1 -1
  74. data/lib/eventmachine.rb +40 -84
  75. data/lib/jeventmachine.rb +2 -1
  76. data/lib/rubyeventmachine.rb +2 -0
  77. data/setup.rb +1585 -1585
  78. data/tasks/doc.rake +30 -0
  79. data/tasks/package.rake +85 -0
  80. data/tasks/test.rake +6 -0
  81. data/tests/client.crt +31 -31
  82. data/tests/client.key +51 -51
  83. data/tests/test_attach.rb +13 -3
  84. data/tests/test_basic.rb +60 -95
  85. data/tests/test_channel.rb +3 -2
  86. data/tests/test_defer.rb +49 -47
  87. data/tests/test_deferrable.rb +35 -0
  88. data/tests/test_error_handler.rb +35 -35
  89. data/tests/test_errors.rb +82 -82
  90. data/tests/test_exc.rb +55 -55
  91. data/tests/test_file_watch.rb +49 -49
  92. data/tests/test_futures.rb +198 -198
  93. data/tests/test_handler_check.rb +36 -36
  94. data/tests/test_hc.rb +190 -218
  95. data/tests/test_httpclient.rb +227 -218
  96. data/tests/test_httpclient2.rb +3 -2
  97. data/tests/test_inactivity_timeout.rb +3 -3
  98. data/tests/test_kb.rb +60 -60
  99. data/tests/test_ltp.rb +13 -5
  100. data/tests/test_ltp2.rb +317 -317
  101. data/tests/test_next_tick.rb +1 -1
  102. data/tests/test_object_protocol.rb +36 -36
  103. data/tests/test_pending_connect_timeout.rb +2 -2
  104. data/tests/test_process_watch.rb +50 -48
  105. data/tests/test_proxy_connection.rb +52 -0
  106. data/tests/test_pure.rb +134 -125
  107. data/tests/test_queue.rb +44 -44
  108. data/tests/test_running.rb +42 -42
  109. data/tests/test_sasl.rb +72 -72
  110. data/tests/test_send_file.rb +251 -242
  111. data/tests/test_servers.rb +76 -76
  112. data/tests/test_smtpclient.rb +83 -83
  113. data/tests/test_smtpserver.rb +85 -85
  114. data/tests/test_spawn.rb +322 -322
  115. data/tests/test_ssl_methods.rb +49 -49
  116. data/tests/test_ssl_verify.rb +82 -82
  117. data/tests/test_tick_loop.rb +59 -0
  118. data/tests/test_timers.rb +13 -15
  119. data/tests/test_ud.rb +36 -36
  120. data/tests/testem.rb +31 -31
  121. metadata +66 -51
  122. data/ext/cplusplus.cpp +0 -202
  123. data/ext/emwin.cpp +0 -300
  124. data/ext/emwin.h +0 -94
  125. data/ext/epoll.cpp +0 -26
  126. data/ext/epoll.h +0 -25
  127. data/ext/eventmachine_cpp.h +0 -96
  128. data/ext/files.cpp +0 -94
  129. data/ext/files.h +0 -65
  130. data/ext/sigs.cpp +0 -89
  131. data/ext/sigs.h +0 -32
  132. data/java/src/com/rubyeventmachine/application/Application.java +0 -194
  133. data/java/src/com/rubyeventmachine/application/Connection.java +0 -74
  134. data/java/src/com/rubyeventmachine/application/ConnectionFactory.java +0 -37
  135. data/java/src/com/rubyeventmachine/application/DefaultConnectionFactory.java +0 -46
  136. data/java/src/com/rubyeventmachine/application/PeriodicTimer.java +0 -38
  137. data/java/src/com/rubyeventmachine/application/Timer.java +0 -54
  138. data/java/src/com/rubyeventmachine/tests/ApplicationTest.java +0 -109
  139. data/java/src/com/rubyeventmachine/tests/ConnectTest.java +0 -148
  140. data/java/src/com/rubyeventmachine/tests/EMTest.java +0 -80
  141. data/java/src/com/rubyeventmachine/tests/TestDatagrams.java +0 -53
  142. data/java/src/com/rubyeventmachine/tests/TestServers.java +0 -75
  143. data/java/src/com/rubyeventmachine/tests/TestTimers.java +0 -90
  144. data/lib/evma.rb +0 -32
  145. data/lib/evma/callback.rb +0 -32
  146. data/lib/evma/container.rb +0 -75
  147. data/lib/evma/factory.rb +0 -77
  148. data/lib/evma/protocol.rb +0 -87
  149. data/lib/evma/reactor.rb +0 -48
  150. data/web/whatis +0 -7
@@ -1,94 +1,94 @@
1
- RUBY/EventMachine RELEASE NOTES
2
-
3
- --------------------------------------------------
4
- Version: 0.9.0, released xxXXX07
5
- Added Erlang-like distributed-computing features
6
-
7
- --------------------------------------------------
8
- Version: 0.8.0, released 23Jun07
9
- Added an epoll implementation for Linux 2.6 kernels.
10
- Added evented #popen.
11
-
12
- --------------------------------------------------
13
- Version: 0.7.3, released 22May07
14
- Added a large variety of small features. See the ChangeLog.
15
-
16
- --------------------------------------------------
17
- Version: 0.7.1, released xxNov06
18
- Added protocol handlers for line-oriented protocols.
19
- Various bug fixes.
20
-
21
- --------------------------------------------------
22
- Version: 0.7.0, released 20Nov06
23
- Added a fix in em.cpp/ConnectToServer to fix a fatal exception that
24
- occurred in FreeBSD when connecting successfully to a remote server.
25
-
26
- --------------------------------------------------
27
- Version: 0.6.0, released xxJul06
28
- Added deferred operations, suggested by Don Stocks, amillionhitpoints@yahoo.com.
29
-
30
- --------------------------------------------------
31
- Version: 0.5.4, released xxJun06
32
- Added get_peername support for streams and datagrams.
33
-
34
- --------------------------------------------------
35
- Version: 0.5.3, released 17May06
36
- Fixed bugs in extconf.rb, thanks to Daniel Harple, dharple@generalconsumption.org.
37
- Added proper setup.rb and rake tasks, thanks to Austin Ziegler.
38
- Fixed a handful of reported problems with builds on various platforms.
39
-
40
- --------------------------------------------------
41
- Version: 0.5.2, released 05May06
42
- Made several nonvisible improvements to the Windows
43
- implementation.
44
- Added an exception-handling patch contributed by Jeff Rose, jeff@rosejn.net.
45
- Added a dir-config patch contributed anonymously.
46
- Supported builds on Solaris.
47
-
48
- --------------------------------------------------
49
- Version: 0.5.1, released 05May06
50
- Made it possible to pass a Class rather than a Module
51
- to a protocol handler.
52
- Added Windows port.
53
-
54
- --------------------------------------------------
55
- Version: 0.5.0, released 30Apr06
56
- Added a preliminary SSL/TLS extension. This will probably
57
- change over the next few releases.
58
-
59
- --------------------------------------------------
60
- Version: 0.4.5, released 29Apr06
61
- Changed ext files so the ruby.h is installed after unistd.h
62
- otherwise it doesn't compile on gcc 4.1
63
-
64
- --------------------------------------------------
65
- Version: 0.4.2, released 19Apr06
66
- Changed the Ruby-glue so the extension will play nicer
67
- in the sandbox with Ruby threads.
68
- Added an EventMachine::run_without_threads API to
69
- switch off the thread-awareness for better performance
70
- in programs that do not spin any Ruby threads.
71
-
72
- --------------------------------------------------
73
- Version: 0.4.1, released 15Apr06
74
- Reworked the shared-object interface to make it easier to
75
- use EventMachine from languages other than Ruby.
76
-
77
- --------------------------------------------------
78
- Version: 0.3.2, released 12Apr06
79
- Added support for a user-supplied block in EventMachine#connect.
80
-
81
- --------------------------------------------------
82
- Version: 0.3.1, released 11Apr06
83
- Fixed bug that prevented EventMachine from being run multiple
84
- times in a single process.
85
-
86
- --------------------------------------------------
87
- Version: 0.3.0, released 10Apr06
88
- Added method EventHandler::Connection::post_init
89
-
90
- --------------------------------------------------
91
- Version: 0.2.0, released 10Apr06
92
- Added method EventHandler::stop
93
-
94
-
1
+ RUBY/EventMachine RELEASE NOTES
2
+
3
+ --------------------------------------------------
4
+ Version: 0.9.0, released xxXXX07
5
+ Added Erlang-like distributed-computing features
6
+
7
+ --------------------------------------------------
8
+ Version: 0.8.0, released 23Jun07
9
+ Added an epoll implementation for Linux 2.6 kernels.
10
+ Added evented #popen.
11
+
12
+ --------------------------------------------------
13
+ Version: 0.7.3, released 22May07
14
+ Added a large variety of small features. See the ChangeLog.
15
+
16
+ --------------------------------------------------
17
+ Version: 0.7.1, released xxNov06
18
+ Added protocol handlers for line-oriented protocols.
19
+ Various bug fixes.
20
+
21
+ --------------------------------------------------
22
+ Version: 0.7.0, released 20Nov06
23
+ Added a fix in em.cpp/ConnectToServer to fix a fatal exception that
24
+ occurred in FreeBSD when connecting successfully to a remote server.
25
+
26
+ --------------------------------------------------
27
+ Version: 0.6.0, released xxJul06
28
+ Added deferred operations, suggested by Don Stocks, amillionhitpoints@yahoo.com.
29
+
30
+ --------------------------------------------------
31
+ Version: 0.5.4, released xxJun06
32
+ Added get_peername support for streams and datagrams.
33
+
34
+ --------------------------------------------------
35
+ Version: 0.5.3, released 17May06
36
+ Fixed bugs in extconf.rb, thanks to Daniel Harple, dharple@generalconsumption.org.
37
+ Added proper setup.rb and rake tasks, thanks to Austin Ziegler.
38
+ Fixed a handful of reported problems with builds on various platforms.
39
+
40
+ --------------------------------------------------
41
+ Version: 0.5.2, released 05May06
42
+ Made several nonvisible improvements to the Windows
43
+ implementation.
44
+ Added an exception-handling patch contributed by Jeff Rose, jeff@rosejn.net.
45
+ Added a dir-config patch contributed anonymously.
46
+ Supported builds on Solaris.
47
+
48
+ --------------------------------------------------
49
+ Version: 0.5.1, released 05May06
50
+ Made it possible to pass a Class rather than a Module
51
+ to a protocol handler.
52
+ Added Windows port.
53
+
54
+ --------------------------------------------------
55
+ Version: 0.5.0, released 30Apr06
56
+ Added a preliminary SSL/TLS extension. This will probably
57
+ change over the next few releases.
58
+
59
+ --------------------------------------------------
60
+ Version: 0.4.5, released 29Apr06
61
+ Changed ext files so the ruby.h is installed after unistd.h
62
+ otherwise it doesn't compile on gcc 4.1
63
+
64
+ --------------------------------------------------
65
+ Version: 0.4.2, released 19Apr06
66
+ Changed the Ruby-glue so the extension will play nicer
67
+ in the sandbox with Ruby threads.
68
+ Added an EventMachine::run_without_threads API to
69
+ switch off the thread-awareness for better performance
70
+ in programs that do not spin any Ruby threads.
71
+
72
+ --------------------------------------------------
73
+ Version: 0.4.1, released 15Apr06
74
+ Reworked the shared-object interface to make it easier to
75
+ use EventMachine from languages other than Ruby.
76
+
77
+ --------------------------------------------------
78
+ Version: 0.3.2, released 12Apr06
79
+ Added support for a user-supplied block in EventMachine#connect.
80
+
81
+ --------------------------------------------------
82
+ Version: 0.3.1, released 11Apr06
83
+ Fixed bug that prevented EventMachine from being run multiple
84
+ times in a single process.
85
+
86
+ --------------------------------------------------
87
+ Version: 0.3.0, released 10Apr06
88
+ Added method EventHandler::Connection::post_init
89
+
90
+ --------------------------------------------------
91
+ Version: 0.2.0, released 10Apr06
92
+ Added method EventHandler::stop
93
+
94
+
data/docs/SMTP CHANGED
@@ -1,2 +1,4 @@
1
- This note details the usage of EventMachine's built-in support for SMTP. EM supports both client and server connections, which will be described in separate sections.
2
-
1
+ This note details the usage of EventMachine's built-in support for SMTP. EM
2
+ supports both client and server connections, which will be described in
3
+ separate sections.
4
+
@@ -1,89 +1,148 @@
1
- EventMachine (EM) adds two different formalisms for lightweight concurrency to the Ruby programmer's toolbox: spawned processes and deferrables. This note will show you how to use spawned processes. For more information, see the separate document LIGHTWEIGHT_CONCURRENCY.
2
-
3
-
4
- === What are Spawned Processes?
5
-
6
- Spawned Processes in EventMachine are inspired directly by the "processes" found in the Erlang programming language. EM deliberately borrows much (but not all) of Erlang's terminology. However, EM's spawned processes differ from Erlang's in ways that reflect not only Ruby style, but also the fact that Ruby is not a functional language like Erlang.
7
-
8
- Let's proceed with a complete, working code sample that we will analyze line by line. Here's an EM implementation of the "ping-pong" program that also appears in the Erlang tutorial:
9
-
10
-
11
- require 'eventmachine'
12
-
13
- EM.run {
14
- pong = EM.spawn {|x, ping|
15
- puts "Pong received #{x}"
16
- ping.notify( x-1 )
17
- }
18
-
19
- ping = EM.spawn {|x|
20
- if x > 0
21
- puts "Pinging #{x}"
22
- pong.notify x, self
23
- else
24
- EM.stop
25
- end
26
- }
27
-
28
- ping.notify 3
29
- }
30
-
31
- If you run this program, you'll see the following output:
32
-
33
- Pinging 3
34
- Pong received 3
35
- Pinging 2
36
- Pong received 2
37
- Pinging 1
38
- Pong received 1
39
-
40
- Let's take it step by step.
41
-
42
- EventMachine#spawn works very much like the built-in function spawn in Erlang. It returns a reference to a Ruby object of class EventMachine::SpawnedProcess, which is actually a schedulable entity. In Erlang, the value returned from spawn is called a "process identifier" or "pid." But we'll refer to the Ruby object returned from EM#spawn simply as a "spawned process."
43
-
44
- You pass a Ruby block with zero or more parameters to EventMachine#spawn. Like all Ruby blocks, this one is a closure, so it can refer to variables defined in the local context when you call EM#spawn.
45
-
46
- However, the code block passed to EM#spawn does NOT execute immediately by default. Rather, it will execute only when the Spawned Object is "notified." In Erlang, this process is called "message passing," and is done with the operator !, but in Ruby it's done simply by calling the #notify method of a spawned-process object. The parameters you pass to #notify must match those defined in the block that was originally passed to EM#spawn.
47
-
48
- When you call the #notify method of a spawned-process object, EM's reactor core will execute the code block originally passed to EM#spawn, at some point in the future. (#notify itself merely adds a notification to the object's message queue and ALWAYS returns immediately.)
49
-
50
- When a SpawnedProcess object executes a notification, it does so in the context of the SpawnedProcess object itself. The notified code block can see local context from the point at which EM#spawn was called. However, the value of "self" inside the notified code block is a reference to the SpawnedProcesss object itself.
51
-
52
- An EM spawned process is nothing more than a Ruby object with a message queue attached to it. You can have any number of spawned processes in your program without compromising scalability. You can notify a spawned process any number of times, and each notification will cause a "message" to be placed in the queue of the spawned process. Spawned processes with non-empty message queues are scheduled for execution automatically by the EM reactor. Spawned processes with no visible references are garbage-collected like any other Ruby object.
53
-
54
- Back to our code sample:
55
-
56
- pong = EM.spawn {|x, ping|
57
- puts "Pong received #{x}"
58
- ping.notify( x-1 )
59
- }
60
-
61
- This simply creates a spawned process and assigns it to the local variable pong. You can see that the spawned code block takes a numeric parameter and a reference to another spawned process. When pong is notified, it expects to receive arguments corresponding to these two parameters. It simply prints out the number it receives as the first argument. Then it notifies the spawned process referenced by the second argument, passing it the first argument minus 1.
62
-
63
- And then the block ends, which is crucial because otherwise nothing else can run. (Remember that in LC, scheduled entities run to completion and are never preempted.)
64
-
65
- On to the next bit of the code sample:
66
-
67
- ping = EM.spawn {|x|
68
- if x > 0
69
- puts "Pinging #{x}"
70
- pong.notify x, self
71
- else
72
- EM.stop
73
- end
74
- }
75
-
76
- Here, we're spawning a process that takes a single (numeric) parameter. If the parameter is greater than zero, the block writes it to the console. It then notifies the spawned process referenced by the pong local variable, passing as arguments its number argument, and a reference to itself. The latter reference, as you saw above, is used by pong to send a return notification.
77
-
78
- If the ping process receives a zero value, it will stop the reactor loop and end the program.
79
-
80
- Now we've created a pair of spawned processes, but nothing else has happened. If we stop now, the program will spin in the EM reactor loop, doing nothing at all. Our spawned processes will never be scheduled for execution.
81
-
82
- But look at the next line in the code sample:
83
-
84
- ping.notify 3
85
-
86
- This line gets the ping-pong ball rolling. We call ping's #notify method, passing the argument 3. This causes a message to be sent to the ping spawned process. The message contains the single argument, and it causes the EM reactor to schedule the ping process. And this in turn results in the execution of the Ruby code block passed to EM#spawn when ping was created. Everything else proceeds as a result of the messages that are subsequently passed to each other by the spawned processes.
87
-
88
- [TODO, present the outbound network i/o use case, and clarify that spawned processes are interleaved with normal i/o operations and don't interfere with them at all. Also, blame Erlang for the confusing term "process"]
89
-
1
+ EventMachine (EM) adds two different formalisms for lightweight concurrency
2
+ to the Ruby programmer's toolbox: spawned processes and deferrables. This
3
+ note will show you how to use spawned processes. For more information, see
4
+ the separate document LIGHTWEIGHT_CONCURRENCY.
5
+
6
+
7
+ === What are Spawned Processes?
8
+
9
+ Spawned Processes in EventMachine are inspired directly by the "processes"
10
+ found in the Erlang programming language. EM deliberately borrows much (but
11
+ not all) of Erlang's terminology. However, EM's spawned processes differ from
12
+ Erlang's in ways that reflect not only Ruby style, but also the fact that
13
+ Ruby is not a functional language like Erlang.
14
+
15
+ Let's proceed with a complete, working code sample that we will analyze line
16
+ by line. Here's an EM implementation of the "ping-pong" program that also
17
+ appears in the Erlang tutorial:
18
+
19
+
20
+ require 'eventmachine'
21
+
22
+ EM.run {
23
+ pong = EM.spawn {|x, ping|
24
+ puts "Pong received #{x}"
25
+ ping.notify( x-1 )
26
+ }
27
+
28
+ ping = EM.spawn {|x|
29
+ if x > 0
30
+ puts "Pinging #{x}"
31
+ pong.notify x, self
32
+ else
33
+ EM.stop
34
+ end
35
+ }
36
+
37
+ ping.notify 3
38
+ }
39
+
40
+ If you run this program, you'll see the following output:
41
+
42
+ Pinging 3
43
+ Pong received 3
44
+ Pinging 2
45
+ Pong received 2
46
+ Pinging 1
47
+ Pong received 1
48
+
49
+ Let's take it step by step.
50
+
51
+ EventMachine#spawn works very much like the built-in function spawn in
52
+ Erlang. It returns a reference to a Ruby object of class
53
+ EventMachine::SpawnedProcess, which is actually a schedulable entity. In
54
+ Erlang, the value returned from spawn is called a "process identifier" or
55
+ "pid." But we'll refer to the Ruby object returned from EM#spawn simply as a
56
+ "spawned process."
57
+
58
+ You pass a Ruby block with zero or more parameters to EventMachine#spawn.
59
+ Like all Ruby blocks, this one is a closure, so it can refer to variables
60
+ defined in the local context when you call EM#spawn.
61
+
62
+ However, the code block passed to EM#spawn does NOT execute immediately by
63
+ default. Rather, it will execute only when the Spawned Object is "notified."
64
+ In Erlang, this process is called "message passing," and is done with the
65
+ operator !, but in Ruby it's done simply by calling the #notify method of a
66
+ spawned-process object. The parameters you pass to #notify must match those
67
+ defined in the block that was originally passed to EM#spawn.
68
+
69
+ When you call the #notify method of a spawned-process object, EM's reactor
70
+ core will execute the code block originally passed to EM#spawn, at some point
71
+ in the future. (#notify itself merely adds a notification to the object's
72
+ message queue and ALWAYS returns immediately.)
73
+
74
+ When a SpawnedProcess object executes a notification, it does so in the
75
+ context of the SpawnedProcess object itself. The notified code block can see
76
+ local context from the point at which EM#spawn was called. However, the value
77
+ of "self" inside the notified code block is a reference to the SpawnedProcesss
78
+ object itself.
79
+
80
+ An EM spawned process is nothing more than a Ruby object with a message
81
+ queue attached to it. You can have any number of spawned processes in your
82
+ program without compromising scalability. You can notify a spawned process
83
+ any number of times, and each notification will cause a "message" to be
84
+ placed in the queue of the spawned process. Spawned processes with non-empty
85
+ message queues are scheduled for execution automatically by the EM reactor.
86
+ Spawned processes with no visible references are garbage-collected like any
87
+ other Ruby object.
88
+
89
+ Back to our code sample:
90
+
91
+ pong = EM.spawn {|x, ping|
92
+ puts "Pong received #{x}"
93
+ ping.notify( x-1 )
94
+ }
95
+
96
+ This simply creates a spawned process and assigns it to the local variable
97
+ pong. You can see that the spawned code block takes a numeric parameter and a
98
+ reference to another spawned process. When pong is notified, it expects to
99
+ receive arguments corresponding to these two parameters. It simply prints out
100
+ the number it receives as the first argument. Then it notifies the spawned
101
+ process referenced by the second argument, passing it the first argument
102
+ minus 1.
103
+
104
+ And then the block ends, which is crucial because otherwise nothing else
105
+ can run. (Remember that in LC, scheduled entities run to completion and are
106
+ never preempted.)
107
+
108
+ On to the next bit of the code sample:
109
+
110
+ ping = EM.spawn {|x|
111
+ if x > 0
112
+ puts "Pinging #{x}"
113
+ pong.notify x, self
114
+ else
115
+ EM.stop
116
+ end
117
+ }
118
+
119
+ Here, we're spawning a process that takes a single (numeric) parameter. If
120
+ the parameter is greater than zero, the block writes it to the console. It
121
+ then notifies the spawned process referenced by the pong local variable,
122
+ passing as arguments its number argument, and a reference to itself. The
123
+ latter reference, as you saw above, is used by pong to send a return
124
+ notification.
125
+
126
+ If the ping process receives a zero value, it will stop the reactor loop and
127
+ end the program.
128
+
129
+ Now we've created a pair of spawned processes, but nothing else has happened.
130
+ If we stop now, the program will spin in the EM reactor loop, doing nothing
131
+ at all. Our spawned processes will never be scheduled for execution.
132
+
133
+ But look at the next line in the code sample:
134
+
135
+ ping.notify 3
136
+
137
+ This line gets the ping-pong ball rolling. We call ping's #notify method,
138
+ passing the argument 3. This causes a message to be sent to the ping spawned
139
+ process. The message contains the single argument, and it causes the EM
140
+ reactor to schedule the ping process. And this in turn results in the
141
+ execution of the Ruby code block passed to EM#spawn when ping was created.
142
+ Everything else proceeds as a result of the messages that are subsequently
143
+ passed to each other by the spawned processes.
144
+
145
+ [TODO, present the outbound network i/o use case, and clarify that spawned
146
+ processes are interleaved with normal i/o operations and don't interfere
147
+ with them at all. Also, blame Erlang for the confusing term "process"]
148
+
data/docs/TODO CHANGED
@@ -1,8 +1,8 @@
1
- TODO List:
2
-
3
- 12Aug06: Noticed by Don Stocks. A TCP connect-request that results
4
- in a failed DNS resolution fires a fatal error back to user code.
5
- Uuuuuugly. We should probably cause an unbind event to get fired
6
- instead, and add some parameterization so the caller can detect
7
- the nature of the failure.
8
-
1
+ TODO List:
2
+
3
+ 12Aug06: Noticed by Don Stocks. A TCP connect-request that results
4
+ in a failed DNS resolution fires a fatal error back to user code.
5
+ Uuuuuugly. We should probably cause an unbind event to get fired
6
+ instead, and add some parameterization so the caller can detect
7
+ the nature of the failure.
8
+