A continuation from my previous Sending Personalized SMS article. Lets imagine telco allows you to send as many messages as you can without limits and you have 100,000 messages to send to all your customers.
If you can only send 1 message per second, it will take almost 28 hours to complete. What if you run it multi threaded with a pool size of 10? That will shorten it down to almost 3 hours to complete the same task.
Say you have the name list of the 100,000 customers in “list.txt”.
+6598765432|Person1
.
.
.
.
.
+6587654321|Person100000
Ruby codes below reads “list.txt”, push it into the queue. Sends SMS multi threaded with a pool size of 10.
POOL_SIZE = 10
def send(strPhoneNumber, strMessage)
`./adb shell service call isms 7 i32 0 s16 "null" s16 "#{strPhoneNumber}" s16 "null" s16 "'#{strMessage}'"`
end
def read_name_list
q = Queue.new
rows=(`cat list.txt`).split("\n")
for z in 0..rows.length-1
row=rows[z]
cells=row.split('|')
h=Hash.new
h['number']=cells[0]
h['name']=cells[1]
q.push(h)
end
return q
end
q = read_name_list
workers = (POOL_SIZE).times.map do
Thread.new do
begin
while h = q.pop(true)
message='Hi %s' % h['name']
send(h['number'],message)
end
rescue ThreadError
end
end
end
workers.map(&:join)
The functioning codes makes explanation of multi threading with pool messy. Let me try to simplify it. I have a queue with 101 numbers, pool size of 10. Multi threading will work on the items in the queue but not in sequence. If you run the code below, you will notice that the number is not in order.
def do_some_task(param)
puts param
end
POOL_SIZE = 10
q = Queue.new
101.times{|i| q.push(i)}
workers = (POOL_SIZE).times.map do
Thread.new do
begin
while n = q.pop(true)
do_some_task(n)
end
rescue ThreadError
end
end
end
workers.map(&:join)