eventmachine 1.0.0.beta.2-x86-mingw32
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +16 -0
- data/Gemfile +1 -0
- data/README +81 -0
- data/Rakefile +11 -0
- data/docs/COPYING +60 -0
- data/docs/ChangeLog +211 -0
- data/docs/DEFERRABLES +246 -0
- data/docs/EPOLL +141 -0
- data/docs/GNU +281 -0
- data/docs/INSTALL +13 -0
- data/docs/KEYBOARD +42 -0
- data/docs/LEGAL +25 -0
- data/docs/LIGHTWEIGHT_CONCURRENCY +130 -0
- data/docs/PURE_RUBY +75 -0
- data/docs/RELEASE_NOTES +94 -0
- data/docs/SMTP +4 -0
- data/docs/SPAWNED_PROCESSES +148 -0
- data/docs/TODO +8 -0
- data/eventmachine.gemspec +33 -0
- data/examples/ex_channel.rb +43 -0
- data/examples/ex_queue.rb +2 -0
- data/examples/ex_tick_loop_array.rb +15 -0
- data/examples/ex_tick_loop_counter.rb +32 -0
- data/examples/helper.rb +2 -0
- data/ext/binder.cpp +124 -0
- data/ext/binder.h +46 -0
- data/ext/cmain.cpp +838 -0
- data/ext/ed.cpp +1884 -0
- data/ext/ed.h +418 -0
- data/ext/em.cpp +2348 -0
- data/ext/em.h +228 -0
- data/ext/eventmachine.h +123 -0
- data/ext/extconf.rb +157 -0
- data/ext/fastfilereader/extconf.rb +85 -0
- data/ext/fastfilereader/mapper.cpp +214 -0
- data/ext/fastfilereader/mapper.h +59 -0
- data/ext/fastfilereader/rubymain.cpp +127 -0
- data/ext/kb.cpp +79 -0
- data/ext/page.cpp +107 -0
- data/ext/page.h +51 -0
- data/ext/pipe.cpp +347 -0
- data/ext/project.h +155 -0
- data/ext/rubymain.cpp +1200 -0
- data/ext/ssl.cpp +460 -0
- data/ext/ssl.h +94 -0
- data/java/.classpath +8 -0
- data/java/.project +17 -0
- data/java/src/com/rubyeventmachine/EmReactor.java +571 -0
- data/java/src/com/rubyeventmachine/EmReactorException.java +40 -0
- data/java/src/com/rubyeventmachine/EventableChannel.java +69 -0
- data/java/src/com/rubyeventmachine/EventableDatagramChannel.java +189 -0
- data/java/src/com/rubyeventmachine/EventableSocketChannel.java +364 -0
- data/lib/em/buftok.rb +138 -0
- data/lib/em/callback.rb +26 -0
- data/lib/em/channel.rb +57 -0
- data/lib/em/connection.rb +569 -0
- data/lib/em/deferrable.rb +206 -0
- data/lib/em/file_watch.rb +54 -0
- data/lib/em/future.rb +61 -0
- data/lib/em/iterator.rb +270 -0
- data/lib/em/messages.rb +66 -0
- data/lib/em/process_watch.rb +44 -0
- data/lib/em/processes.rb +119 -0
- data/lib/em/protocols.rb +36 -0
- data/lib/em/protocols/header_and_content.rb +138 -0
- data/lib/em/protocols/httpclient.rb +268 -0
- data/lib/em/protocols/httpclient2.rb +590 -0
- data/lib/em/protocols/line_and_text.rb +125 -0
- data/lib/em/protocols/line_protocol.rb +28 -0
- data/lib/em/protocols/linetext2.rb +161 -0
- data/lib/em/protocols/memcache.rb +323 -0
- data/lib/em/protocols/object_protocol.rb +45 -0
- data/lib/em/protocols/postgres3.rb +247 -0
- data/lib/em/protocols/saslauth.rb +175 -0
- data/lib/em/protocols/smtpclient.rb +357 -0
- data/lib/em/protocols/smtpserver.rb +640 -0
- data/lib/em/protocols/socks4.rb +66 -0
- data/lib/em/protocols/stomp.rb +200 -0
- data/lib/em/protocols/tcptest.rb +53 -0
- data/lib/em/pure_ruby.rb +1013 -0
- data/lib/em/queue.rb +62 -0
- data/lib/em/spawnable.rb +85 -0
- data/lib/em/streamer.rb +130 -0
- data/lib/em/tick_loop.rb +85 -0
- data/lib/em/timers.rb +57 -0
- data/lib/em/version.rb +3 -0
- data/lib/eventmachine.rb +1548 -0
- data/lib/jeventmachine.rb +258 -0
- data/lib/rubyeventmachine.rb +2 -0
- data/setup.rb +1585 -0
- data/tasks/cpp.rake_example +77 -0
- data/tasks/doc.rake +30 -0
- data/tasks/package.rake +85 -0
- data/tasks/test.rake +6 -0
- data/tests/client.crt +31 -0
- data/tests/client.key +51 -0
- data/tests/test_attach.rb +136 -0
- data/tests/test_basic.rb +249 -0
- data/tests/test_channel.rb +64 -0
- data/tests/test_connection_count.rb +35 -0
- data/tests/test_defer.rb +49 -0
- data/tests/test_deferrable.rb +35 -0
- data/tests/test_epoll.rb +160 -0
- data/tests/test_error_handler.rb +35 -0
- data/tests/test_errors.rb +82 -0
- data/tests/test_exc.rb +55 -0
- data/tests/test_file_watch.rb +49 -0
- data/tests/test_futures.rb +198 -0
- data/tests/test_get_sock_opt.rb +30 -0
- data/tests/test_handler_check.rb +37 -0
- data/tests/test_hc.rb +190 -0
- data/tests/test_httpclient.rb +227 -0
- data/tests/test_httpclient2.rb +154 -0
- data/tests/test_inactivity_timeout.rb +50 -0
- data/tests/test_kb.rb +60 -0
- data/tests/test_ltp.rb +190 -0
- data/tests/test_ltp2.rb +317 -0
- data/tests/test_next_tick.rb +133 -0
- data/tests/test_object_protocol.rb +37 -0
- data/tests/test_pause.rb +70 -0
- data/tests/test_pending_connect_timeout.rb +48 -0
- data/tests/test_process_watch.rb +50 -0
- data/tests/test_processes.rb +128 -0
- data/tests/test_proxy_connection.rb +144 -0
- data/tests/test_pure.rb +134 -0
- data/tests/test_queue.rb +44 -0
- data/tests/test_running.rb +42 -0
- data/tests/test_sasl.rb +72 -0
- data/tests/test_send_file.rb +251 -0
- data/tests/test_servers.rb +76 -0
- data/tests/test_smtpclient.rb +83 -0
- data/tests/test_smtpserver.rb +85 -0
- data/tests/test_spawn.rb +322 -0
- data/tests/test_ssl_args.rb +79 -0
- data/tests/test_ssl_methods.rb +50 -0
- data/tests/test_ssl_verify.rb +82 -0
- data/tests/test_tick_loop.rb +59 -0
- data/tests/test_timers.rb +160 -0
- data/tests/test_ud.rb +36 -0
- data/tests/testem.rb +31 -0
- metadata +240 -0
@@ -0,0 +1,206 @@
|
|
1
|
+
#--
|
2
|
+
#
|
3
|
+
# Author:: Francis Cianfrocca (gmail: blackhedd)
|
4
|
+
# Homepage:: http://rubyeventmachine.com
|
5
|
+
# Date:: 16 Jul 2006
|
6
|
+
#
|
7
|
+
# See EventMachine and EventMachine::Connection for documentation and
|
8
|
+
# usage examples.
|
9
|
+
#
|
10
|
+
#----------------------------------------------------------------------------
|
11
|
+
#
|
12
|
+
# Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
|
13
|
+
# Gmail: blackhedd
|
14
|
+
#
|
15
|
+
# This program is free software; you can redistribute it and/or modify
|
16
|
+
# it under the terms of either: 1) the GNU General Public License
|
17
|
+
# as published by the Free Software Foundation; either version 2 of the
|
18
|
+
# License, or (at your option) any later version; or 2) Ruby's License.
|
19
|
+
#
|
20
|
+
# See the file COPYING for complete licensing information.
|
21
|
+
#
|
22
|
+
#---------------------------------------------------------------------------
|
23
|
+
#
|
24
|
+
#
|
25
|
+
|
26
|
+
module EventMachine
|
27
|
+
module Deferrable
|
28
|
+
|
29
|
+
# Specify a block to be executed if and when the Deferrable object receives
|
30
|
+
# a status of :succeeded. See #set_deferred_status for more information.
|
31
|
+
#
|
32
|
+
# Calling this method on a Deferrable object whose status is not yet known
|
33
|
+
# will cause the callback block to be stored on an internal list.
|
34
|
+
# If you call this method on a Deferrable whose status is :succeeded, the
|
35
|
+
# block will be executed immediately, receiving the parameters given to the
|
36
|
+
# prior #set_deferred_status call.
|
37
|
+
#
|
38
|
+
#--
|
39
|
+
# If there is no status, add a callback to an internal list.
|
40
|
+
# If status is succeeded, execute the callback immediately.
|
41
|
+
# If status is failed, do nothing.
|
42
|
+
#
|
43
|
+
def callback &block
|
44
|
+
return unless block
|
45
|
+
@deferred_status ||= :unknown
|
46
|
+
if @deferred_status == :succeeded
|
47
|
+
block.call(*@deferred_args)
|
48
|
+
elsif @deferred_status != :failed
|
49
|
+
@callbacks ||= []
|
50
|
+
@callbacks.unshift block # << block
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
# Cancels an outstanding callback to &block if any. Undoes the action of #callback.
|
55
|
+
#
|
56
|
+
def cancel_callback block
|
57
|
+
@callbacks ||= []
|
58
|
+
@callbacks.delete block
|
59
|
+
end
|
60
|
+
|
61
|
+
# Specify a block to be executed if and when the Deferrable object receives
|
62
|
+
# a status of :failed. See #set_deferred_status for more information.
|
63
|
+
#--
|
64
|
+
# If there is no status, add an errback to an internal list.
|
65
|
+
# If status is failed, execute the errback immediately.
|
66
|
+
# If status is succeeded, do nothing.
|
67
|
+
#
|
68
|
+
def errback &block
|
69
|
+
return unless block
|
70
|
+
@deferred_status ||= :unknown
|
71
|
+
if @deferred_status == :failed
|
72
|
+
block.call(*@deferred_args)
|
73
|
+
elsif @deferred_status != :succeeded
|
74
|
+
@errbacks ||= []
|
75
|
+
@errbacks.unshift block # << block
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
# Cancels an outstanding errback to &block if any. Undoes the action of #errback.
|
80
|
+
#
|
81
|
+
def cancel_errback block
|
82
|
+
@errbacks ||= []
|
83
|
+
@errbacks.delete block
|
84
|
+
end
|
85
|
+
|
86
|
+
# Sets the "disposition" (status) of the Deferrable object. See also the large set of
|
87
|
+
# sugarings for this method.
|
88
|
+
# Note that if you call this method without arguments,
|
89
|
+
# no arguments will be passed to the callback/errback.
|
90
|
+
# If the user has coded these with arguments, then the
|
91
|
+
# user code will throw an argument exception.
|
92
|
+
# Implementors of deferrable classes <b>must</b>
|
93
|
+
# document the arguments they will supply to user callbacks.
|
94
|
+
#
|
95
|
+
# OBSERVE SOMETHING VERY SPECIAL here: you may call this method even
|
96
|
+
# on the INSIDE of a callback. This is very useful when a previously-registered
|
97
|
+
# callback wants to change the parameters that will be passed to subsequently-registered
|
98
|
+
# ones.
|
99
|
+
#
|
100
|
+
# You may give either :succeeded or :failed as the status argument.
|
101
|
+
#
|
102
|
+
# If you pass :succeeded, then all of the blocks passed to the object using the #callback
|
103
|
+
# method (if any) will be executed BEFORE the #set_deferred_status method returns. All of the blocks
|
104
|
+
# passed to the object using #errback will be discarded.
|
105
|
+
#
|
106
|
+
# If you pass :failed, then all of the blocks passed to the object using the #errback
|
107
|
+
# method (if any) will be executed BEFORE the #set_deferred_status method returns. All of the blocks
|
108
|
+
# passed to the object using # callback will be discarded.
|
109
|
+
#
|
110
|
+
# If you pass any arguments to #set_deferred_status in addition to the status argument,
|
111
|
+
# they will be passed as arguments to any callbacks or errbacks that are executed.
|
112
|
+
# It's your responsibility to ensure that the argument lists specified in your callbacks and
|
113
|
+
# errbacks match the arguments given in calls to #set_deferred_status, otherwise Ruby will raise
|
114
|
+
# an ArgumentError.
|
115
|
+
#
|
116
|
+
#--
|
117
|
+
# We're shifting callbacks off and discarding them as we execute them.
|
118
|
+
# This is valid because by definition callbacks are executed no more than
|
119
|
+
# once. It also has the magic effect of permitting recursive calls, which
|
120
|
+
# means that a callback can call #set_deferred_status and change the parameters
|
121
|
+
# that will be sent to subsequent callbacks down the chain.
|
122
|
+
#
|
123
|
+
# Changed @callbacks and @errbacks from push/shift to unshift/pop, per suggestion
|
124
|
+
# by Kirk Haines, to work around the memory leak bug that still exists in many Ruby
|
125
|
+
# versions.
|
126
|
+
#
|
127
|
+
# Changed 15Sep07: after processing callbacks or errbacks, CLEAR the other set of
|
128
|
+
# handlers. This gets us a little closer to the behavior of Twisted's "deferred,"
|
129
|
+
# which only allows status to be set once. Prior to making this change, it was possible
|
130
|
+
# to "succeed" a Deferrable (triggering its callbacks), and then immediately "fail" it,
|
131
|
+
# triggering its errbacks! That is clearly undesirable, but it's just as undesirable
|
132
|
+
# to raise an exception is status is set more than once on a Deferrable. The latter
|
133
|
+
# behavior would invalidate the idiom of resetting arguments by setting status from
|
134
|
+
# within a callback or errback, but more seriously it would cause spurious errors
|
135
|
+
# if a Deferrable was timed out and then an attempt was made to succeed it. See the
|
136
|
+
# comments under the new method #timeout.
|
137
|
+
#
|
138
|
+
def set_deferred_status status, *args
|
139
|
+
cancel_timeout
|
140
|
+
@errbacks ||= nil
|
141
|
+
@callbacks ||= nil
|
142
|
+
@deferred_status = status
|
143
|
+
@deferred_args = args
|
144
|
+
case @deferred_status
|
145
|
+
when :succeeded
|
146
|
+
if @callbacks
|
147
|
+
while cb = @callbacks.pop
|
148
|
+
cb.call(*@deferred_args)
|
149
|
+
end
|
150
|
+
end
|
151
|
+
@errbacks.clear if @errbacks
|
152
|
+
when :failed
|
153
|
+
if @errbacks
|
154
|
+
while eb = @errbacks.pop
|
155
|
+
eb.call(*@deferred_args)
|
156
|
+
end
|
157
|
+
end
|
158
|
+
@callbacks.clear if @callbacks
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
|
163
|
+
# Setting a timeout on a Deferrable causes it to go into the failed state after
|
164
|
+
# the Timeout expires (passing no arguments to the object's errbacks).
|
165
|
+
# Setting the status at any time prior to a call to the expiration of the timeout
|
166
|
+
# will cause the timer to be cancelled.
|
167
|
+
def timeout seconds, *args
|
168
|
+
cancel_timeout
|
169
|
+
me = self
|
170
|
+
@deferred_timeout = EventMachine::Timer.new(seconds) {me.fail(*args)}
|
171
|
+
end
|
172
|
+
|
173
|
+
# Cancels an outstanding timeout if any. Undoes the action of #timeout.
|
174
|
+
#
|
175
|
+
def cancel_timeout
|
176
|
+
@deferred_timeout ||= nil
|
177
|
+
if @deferred_timeout
|
178
|
+
@deferred_timeout.cancel
|
179
|
+
@deferred_timeout = nil
|
180
|
+
end
|
181
|
+
end
|
182
|
+
|
183
|
+
|
184
|
+
# Sugar for set_deferred_status(:succeeded, ...)
|
185
|
+
#
|
186
|
+
def succeed *args
|
187
|
+
set_deferred_status :succeeded, *args
|
188
|
+
end
|
189
|
+
alias set_deferred_success succeed
|
190
|
+
|
191
|
+
# Sugar for set_deferred_status(:failed, ...)
|
192
|
+
#
|
193
|
+
def fail *args
|
194
|
+
set_deferred_status :failed, *args
|
195
|
+
end
|
196
|
+
alias set_deferred_failure fail
|
197
|
+
end
|
198
|
+
|
199
|
+
|
200
|
+
# DefaultDeferrable is an otherwise empty class that includes Deferrable.
|
201
|
+
# This is very useful when you just need to return a Deferrable object
|
202
|
+
# as a way of communicating deferred status to some other part of a program.
|
203
|
+
class DefaultDeferrable
|
204
|
+
include Deferrable
|
205
|
+
end
|
206
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
module EventMachine
|
2
|
+
|
3
|
+
# This is subclassed from EventMachine::Connection for use with the file monitoring API. Read the
|
4
|
+
# documentation on the instance methods of this class, and for a full explanation see EventMachine.watch_file.
|
5
|
+
class FileWatch < Connection
|
6
|
+
# :stopdoc:
|
7
|
+
Cmodified = 'modified'.freeze
|
8
|
+
Cdeleted = 'deleted'.freeze
|
9
|
+
Cmoved = 'moved'.freeze
|
10
|
+
# :startdoc:
|
11
|
+
|
12
|
+
def receive_data(data) #:nodoc:
|
13
|
+
case data
|
14
|
+
when Cmodified
|
15
|
+
file_modified
|
16
|
+
when Cdeleted
|
17
|
+
file_deleted
|
18
|
+
when Cmoved
|
19
|
+
file_moved
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
# Returns the path that EventMachine::watch_file was originally called with. The current implementation
|
24
|
+
# does not pick up on the new filename after a rename occurs.
|
25
|
+
def path
|
26
|
+
@path
|
27
|
+
end
|
28
|
+
|
29
|
+
# Should be redefined with the user's custom callback that will be fired when the file is modified.
|
30
|
+
def file_modified
|
31
|
+
end
|
32
|
+
|
33
|
+
# Should be redefined with the user's custom callback that will be fired when the file is deleted.
|
34
|
+
# When the file is deleted, stop_watching will be called after this to make sure everything is
|
35
|
+
# cleaned up correctly.
|
36
|
+
#
|
37
|
+
# Note that on linux (with inotify), file_deleted will not be called until all open file descriptors to
|
38
|
+
# the file have been closed.
|
39
|
+
def file_deleted
|
40
|
+
end
|
41
|
+
|
42
|
+
# Should be redefined with the user's custom callback that will be fired when the file is moved or renamed.
|
43
|
+
def file_moved
|
44
|
+
end
|
45
|
+
|
46
|
+
# Discontinue monitoring of the file.
|
47
|
+
# This involves cleaning up the underlying monitoring details with kqueue/inotify, and in turn firing unbind.
|
48
|
+
# This will be called automatically when a file is deleted. User code may call it as well.
|
49
|
+
def stop_watching
|
50
|
+
EventMachine::unwatch_filename(@signature)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
data/lib/em/future.rb
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
#--
|
2
|
+
#
|
3
|
+
# Author:: Francis Cianfrocca (gmail: blackhedd)
|
4
|
+
# Homepage:: http://rubyeventmachine.com
|
5
|
+
# Date:: 16 Jul 2006
|
6
|
+
#
|
7
|
+
# See EventMachine and EventMachine::Connection for documentation and
|
8
|
+
# usage examples.
|
9
|
+
#
|
10
|
+
#----------------------------------------------------------------------------
|
11
|
+
#
|
12
|
+
# Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
|
13
|
+
# Gmail: blackhedd
|
14
|
+
#
|
15
|
+
# This program is free software; you can redistribute it and/or modify
|
16
|
+
# it under the terms of either: 1) the GNU General Public License
|
17
|
+
# as published by the Free Software Foundation; either version 2 of the
|
18
|
+
# License, or (at your option) any later version; or 2) Ruby's License.
|
19
|
+
#
|
20
|
+
# See the file COPYING for complete licensing information.
|
21
|
+
#
|
22
|
+
#---------------------------------------------------------------------------
|
23
|
+
#
|
24
|
+
#
|
25
|
+
|
26
|
+
#--
|
27
|
+
# This defines EventMachine::Deferrable#future, which requires
|
28
|
+
# that the rest of EventMachine::Deferrable has already been seen.
|
29
|
+
# (It's in deferrable.rb.)
|
30
|
+
|
31
|
+
module EventMachine
|
32
|
+
module Deferrable
|
33
|
+
|
34
|
+
# A future is a sugaring of a typical deferrable usage.
|
35
|
+
#--
|
36
|
+
# Evaluate arg (which may be an expression or a block).
|
37
|
+
# What's the class of arg?
|
38
|
+
# If arg is an ordinary expression, then return it.
|
39
|
+
# If arg is deferrable (responds to :set_deferred_status),
|
40
|
+
# then look at the arguments. If either callback or errback
|
41
|
+
# are defined, then use them. If neither are defined, then
|
42
|
+
# use the supplied block (if any) as the callback.
|
43
|
+
# Then return arg.
|
44
|
+
def self.future arg, cb=nil, eb=nil, &blk
|
45
|
+
arg = arg.call if arg.respond_to?(:call)
|
46
|
+
|
47
|
+
if arg.respond_to?(:set_deferred_status)
|
48
|
+
if cb || eb
|
49
|
+
arg.callback(&cb) if cb
|
50
|
+
arg.errback(&eb) if eb
|
51
|
+
else
|
52
|
+
arg.callback(&blk) if blk
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
arg
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
data/lib/em/iterator.rb
ADDED
@@ -0,0 +1,270 @@
|
|
1
|
+
module EventMachine
|
2
|
+
# A simple iterator for concurrent asynchronous work.
|
3
|
+
#
|
4
|
+
# Unlike ruby's built-in iterators, the end of the current iteration cycle is signaled manually,
|
5
|
+
# instead of happening automatically after the yielded block finishes executing. For example:
|
6
|
+
#
|
7
|
+
# (0..10).each{ |num| }
|
8
|
+
#
|
9
|
+
# becomes:
|
10
|
+
#
|
11
|
+
# EM::Iterator.new(0..10).each{ |num,iter| iter.next }
|
12
|
+
#
|
13
|
+
# This is especially useful when doing asynchronous work via reactor libraries and
|
14
|
+
# functions. For example, given a sync and async http api:
|
15
|
+
#
|
16
|
+
# response = sync_http_get(url); ...
|
17
|
+
# async_http_get(url){ |response| ... }
|
18
|
+
#
|
19
|
+
# a synchronous iterator such as:
|
20
|
+
#
|
21
|
+
# responses = urls.map{ |url| sync_http_get(url) }
|
22
|
+
# ...
|
23
|
+
# puts 'all done!'
|
24
|
+
#
|
25
|
+
# could be written as:
|
26
|
+
#
|
27
|
+
# EM::Iterator.new(urls).map(proc{ |url,iter|
|
28
|
+
# async_http_get(url){ |res|
|
29
|
+
# iter.return(res)
|
30
|
+
# }
|
31
|
+
# }, proc{ |responses|
|
32
|
+
# ...
|
33
|
+
# puts 'all done!'
|
34
|
+
# })
|
35
|
+
#
|
36
|
+
# Now, you can take advantage of the asynchronous api to issue requests in parallel. For example,
|
37
|
+
# to fetch 10 urls at a time, simply pass in a concurrency of 10:
|
38
|
+
#
|
39
|
+
# EM::Iterator.new(urls, 10).each do |url,iter|
|
40
|
+
# async_http_get(url){ iter.next }
|
41
|
+
# end
|
42
|
+
#
|
43
|
+
class Iterator
|
44
|
+
# Create a new parallel async iterator with specified concurrency.
|
45
|
+
#
|
46
|
+
# i = EM::Iterator.new(1..100, 10)
|
47
|
+
#
|
48
|
+
# will create an iterator over the range that processes 10 items at a time. Iteration
|
49
|
+
# is started via #each, #map or #inject
|
50
|
+
#
|
51
|
+
def initialize(list, concurrency = 1)
|
52
|
+
raise ArgumentError, 'argument must be an array' unless list.respond_to?(:to_a)
|
53
|
+
@list = list.to_a.dup
|
54
|
+
@concurrency = concurrency
|
55
|
+
|
56
|
+
@started = false
|
57
|
+
@ended = false
|
58
|
+
end
|
59
|
+
|
60
|
+
# Change the concurrency of this iterator. Workers will automatically be spawned or destroyed
|
61
|
+
# to accomodate the new concurrency level.
|
62
|
+
#
|
63
|
+
def concurrency=(val)
|
64
|
+
old = @concurrency
|
65
|
+
@concurrency = val
|
66
|
+
|
67
|
+
spawn_workers if val > old and @started and !@ended
|
68
|
+
end
|
69
|
+
attr_reader :concurrency
|
70
|
+
|
71
|
+
# Iterate over a set of items using the specified block or proc.
|
72
|
+
#
|
73
|
+
# EM::Iterator.new(1..100).each do |num, iter|
|
74
|
+
# puts num
|
75
|
+
# iter.next
|
76
|
+
# end
|
77
|
+
#
|
78
|
+
# An optional second proc is invoked after the iteration is complete.
|
79
|
+
#
|
80
|
+
# EM::Iterator.new(1..100).each(
|
81
|
+
# proc{ |num,iter| iter.next },
|
82
|
+
# proc{ puts 'all done' }
|
83
|
+
# )
|
84
|
+
#
|
85
|
+
def each(foreach=nil, after=nil, &blk)
|
86
|
+
raise ArgumentError, 'proc or block required for iteration' unless foreach ||= blk
|
87
|
+
raise RuntimeError, 'cannot iterate over an iterator more than once' if @started or @ended
|
88
|
+
|
89
|
+
@started = true
|
90
|
+
@pending = 0
|
91
|
+
@workers = 0
|
92
|
+
|
93
|
+
all_done = proc{
|
94
|
+
after.call if after and @ended and @pending == 0
|
95
|
+
}
|
96
|
+
|
97
|
+
@process_next = proc{
|
98
|
+
# p [:process_next, :pending=, @pending, :workers=, @workers, :ended=, @ended, :concurrency=, @concurrency, :list=, @list]
|
99
|
+
unless @ended or @workers > @concurrency
|
100
|
+
if @list.empty?
|
101
|
+
@ended = true
|
102
|
+
@workers -= 1
|
103
|
+
all_done.call
|
104
|
+
else
|
105
|
+
item = @list.shift
|
106
|
+
@pending += 1
|
107
|
+
|
108
|
+
is_done = false
|
109
|
+
on_done = proc{
|
110
|
+
raise RuntimeError, 'already completed this iteration' if is_done
|
111
|
+
is_done = true
|
112
|
+
|
113
|
+
@pending -= 1
|
114
|
+
|
115
|
+
if @ended
|
116
|
+
all_done.call
|
117
|
+
else
|
118
|
+
EM.next_tick(@process_next)
|
119
|
+
end
|
120
|
+
}
|
121
|
+
class << on_done
|
122
|
+
alias :next :call
|
123
|
+
end
|
124
|
+
|
125
|
+
foreach.call(item, on_done)
|
126
|
+
end
|
127
|
+
else
|
128
|
+
@workers -= 1
|
129
|
+
end
|
130
|
+
}
|
131
|
+
|
132
|
+
spawn_workers
|
133
|
+
|
134
|
+
self
|
135
|
+
end
|
136
|
+
|
137
|
+
# Collect the results of an asynchronous iteration into an array.
|
138
|
+
#
|
139
|
+
# EM::Iterator.new(%w[ pwd uptime uname date ], 2).map(proc{ |cmd,iter|
|
140
|
+
# EM.system(cmd){ |output,status|
|
141
|
+
# iter.return(output)
|
142
|
+
# }
|
143
|
+
# }, proc{ |results|
|
144
|
+
# p results
|
145
|
+
# })
|
146
|
+
#
|
147
|
+
def map(foreach, after)
|
148
|
+
index = 0
|
149
|
+
|
150
|
+
inject([], proc{ |results,item,iter|
|
151
|
+
i = index
|
152
|
+
index += 1
|
153
|
+
|
154
|
+
is_done = false
|
155
|
+
on_done = proc{ |res|
|
156
|
+
raise RuntimeError, 'already returned a value for this iteration' if is_done
|
157
|
+
is_done = true
|
158
|
+
|
159
|
+
results[i] = res
|
160
|
+
iter.return(results)
|
161
|
+
}
|
162
|
+
class << on_done
|
163
|
+
alias :return :call
|
164
|
+
def next
|
165
|
+
raise NoMethodError, 'must call #return on a map iterator'
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
foreach.call(item, on_done)
|
170
|
+
}, proc{ |results|
|
171
|
+
after.call(results)
|
172
|
+
})
|
173
|
+
end
|
174
|
+
|
175
|
+
# Inject the results of an asynchronous iteration onto a given object.
|
176
|
+
#
|
177
|
+
# EM::Iterator.new(%w[ pwd uptime uname date ], 2).inject({}, proc{ |hash,cmd,iter|
|
178
|
+
# EM.system(cmd){ |output,status|
|
179
|
+
# hash[cmd] = status.exitstatus == 0 ? output.strip : nil
|
180
|
+
# iter.return(hash)
|
181
|
+
# }
|
182
|
+
# }, proc{ |results|
|
183
|
+
# p results
|
184
|
+
# })
|
185
|
+
#
|
186
|
+
def inject(obj, foreach, after)
|
187
|
+
each(proc{ |item,iter|
|
188
|
+
is_done = false
|
189
|
+
on_done = proc{ |res|
|
190
|
+
raise RuntimeError, 'already returned a value for this iteration' if is_done
|
191
|
+
is_done = true
|
192
|
+
|
193
|
+
obj = res
|
194
|
+
iter.next
|
195
|
+
}
|
196
|
+
class << on_done
|
197
|
+
alias :return :call
|
198
|
+
def next
|
199
|
+
raise NoMethodError, 'must call #return on an inject iterator'
|
200
|
+
end
|
201
|
+
end
|
202
|
+
|
203
|
+
foreach.call(obj, item, on_done)
|
204
|
+
}, proc{
|
205
|
+
after.call(obj)
|
206
|
+
})
|
207
|
+
end
|
208
|
+
|
209
|
+
private
|
210
|
+
|
211
|
+
# Spawn workers to consume items from the iterator's enumerator based on the current concurrency level.
|
212
|
+
#
|
213
|
+
def spawn_workers
|
214
|
+
EM.next_tick(start_worker = proc{
|
215
|
+
if @workers < @concurrency and !@ended
|
216
|
+
# p [:spawning_worker, :workers=, @workers, :concurrency=, @concurrency, :ended=, @ended]
|
217
|
+
@workers += 1
|
218
|
+
@process_next.call
|
219
|
+
EM.next_tick(start_worker)
|
220
|
+
end
|
221
|
+
})
|
222
|
+
nil
|
223
|
+
end
|
224
|
+
end
|
225
|
+
end
|
226
|
+
|
227
|
+
if __FILE__ == $0
|
228
|
+
$:.unshift File.join(File.dirname(__FILE__), '..')
|
229
|
+
require 'eventmachine'
|
230
|
+
|
231
|
+
# TODO: real tests
|
232
|
+
# TODO: pass in one object instead of two? .each{ |iter| puts iter.current; iter.next }
|
233
|
+
# TODO: support iter.pause/resume/stop/break/continue?
|
234
|
+
# TODO: create some exceptions instead of using RuntimeError
|
235
|
+
# TODO: support proc instead of enumerable? EM::Iterator.new(proc{ return queue.pop })
|
236
|
+
|
237
|
+
EM.run{
|
238
|
+
EM::Iterator.new(1..50).each{ |num,iter| p num; iter.next }
|
239
|
+
EM::Iterator.new([1,2,3], 10).each{ |num,iter| p num; iter.next }
|
240
|
+
|
241
|
+
i = EM::Iterator.new(1..100, 5)
|
242
|
+
i.each(proc{|num,iter|
|
243
|
+
p num.to_s
|
244
|
+
iter.next
|
245
|
+
}, proc{
|
246
|
+
p :done
|
247
|
+
})
|
248
|
+
EM.add_timer(0.03){
|
249
|
+
i.concurrency = 1
|
250
|
+
}
|
251
|
+
EM.add_timer(0.04){
|
252
|
+
i.concurrency = 3
|
253
|
+
}
|
254
|
+
|
255
|
+
EM::Iterator.new(100..150).map(proc{ |num,iter|
|
256
|
+
EM.add_timer(0.01){ iter.return(num) }
|
257
|
+
}, proc{ |results|
|
258
|
+
p results
|
259
|
+
})
|
260
|
+
|
261
|
+
EM::Iterator.new(%w[ pwd uptime uname date ], 2).inject({}, proc{ |hash,cmd,iter|
|
262
|
+
EM.system(cmd){ |output,status|
|
263
|
+
hash[cmd] = status.exitstatus == 0 ? output.strip : nil
|
264
|
+
iter.return(hash)
|
265
|
+
}
|
266
|
+
}, proc{ |results|
|
267
|
+
p results
|
268
|
+
})
|
269
|
+
}
|
270
|
+
end
|