pkieltyka (owner)

Revisions

gist: 228064 Download_button fork
public
Public Clone URL: git://gist.github.com/228064.git
Embed All Files: show embed
Ruby #
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
require 'coxswain'
require 'open-uri'
 
pool = Coxswain.pool(10) do |url|
  open(url){|socket| socket.read}
end
 
url = 'http://codeforpeople.com'
n = 20
 
# Here the master dispatches work to the pool of workers
n.times { pool.dispatch(url) }
 
 
##########################################################
 
# another example to dispatch jobs could look like:
 
loop do
   job = Job.fetch #blocks and waits for a new job
   pool.dispatch(job)
end
# this example isn't good because it will make the master block
# and not be able to do anything else, so either you can use
# the example below which doesn't block the main thread,
# or we need a helper method that makes a job loop in a thread
# listening for jobs... or maybe someone has a better idea?
 
 
# or with EM
 
EM.run {
   @queue = EMJack::Connection.new
 
   @queue.each_job do |job|
       ret = pool.dispatch(job)
       @queue.delete(job) if ret
   end
}