Home How to perform asynchronous tasks in fixed interval of time
Reply: 1

How to perform asynchronous tasks in fixed interval of time

aks
1#
aks Published in 2018-01-11 06:11:59Z

The goal is to perform an async task(file read, network operation) without blocking the code. And we have multiple such async tasks that need to be executed at a fixed interval of times. Here is a pseudo code to demonstrate the same.

# the async tasks should be performed in parallel 
# provide me with a return value after the task is complete, or they can have a callback or any other mechanism of communication
async_task_1 = perform_async(1)

# now I need to wait fix amount of time before the async task 2
sleep(5)

# this also similar to the tasks one in nature
async_task_2 = perform_async(2)

# finally do something with the result

I'm reading that in ruby I've 2 options forking, threading. The is also something called as Fiber. I also read that due to GIL in the basic Ruby, I won't be able to make much use of threading. I still want to stick to the base Ruby.

I've written some parallel code previously in OMP and Cuda. But I've never got a chance to do that in Ruby.

Can you suggest how to achieve this?

Shimu
2#
Shimu Reply to 2018-01-11 10:41:47Z

I would recommend to you the concurrent-ruby gem with its async feature. This will work great, as long as your tasks are IO bound. (As you said they are)
There you have a async feature to perform your tasks. To wait the amount of time between your 2 async calls you can use literally the sleep function

class AsyncCalls
  include Concurrent::Asnyc

  def perform_task(params)
    # IO bound task
  end
end

AsyncCalls.new.async.perform_task("param")
sleep 5
AsyncCalls.new.async.perform_task("other param")
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.326615 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO