Sha256: 0ba53fc9a8fa97a6c4ace99421f3c59e2699e18db15c0cf0510b5ae194d9a2c3

Contents?: true

Size: 1.75 KB

Versions: 2

Compression:

Stored size: 1.75 KB

Contents

# Require Gemfile gems
require_relative "../lib/asynchronous"

# you can use simple :c also instead of :concurrency
# remember :concurrency is all about GIL case, so
# you can modify the objects in memory
# This is ideal for little operations in simultaneously or
# when you need to update objects in the memory
calculation = async :concurrency do

  sleep 2
  4 * 2

end
puts "hello concurrency"

calculation.value += 1

puts calculation.value

#>--------------------------------------------------
# or you can use simple {} without sym and this will be by default a
# :concurrency pattern

calculation = async { sleep 3; 4 * 3 }

puts "hello simple concurrency"

calculation.value += 1

# remember you have to use .value to cal the return value from the code block!
puts calculation.value


#>--------------------------------------------------
# now let's see the Parallelism
# you can use simple :p or :parallelism as nametag
# remember :parallelism is all about real OS thread case, so
# you CANT modify the objects in memory only in sharedmemories,
# the normal variables will only be copy on write modify
# This is ideal for big operations where you need do a big process
# w/o the fear of the Garbage collector slowness or the GIL lock
# when you need to update objects in the memory use SharedMemory
calculation = async :parallelism do

  sleep 4
  4 * 5

end
puts "hello parallelism"

calculation.value += 1

puts calculation.value

#>--------------------------------------------------

# more complex way

puts "mixed usecase with arrays as return obj"
calc1 = async :parallelism do

  sleep 4
  # some big database processing brutal memory eater stuff
  [4*5,"hy"]

end

calc2 = async {
  [5+1,"sup!"]
}

puts calc1.value == calc2.value
puts (calc1.value+calc2.value).inspect

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
asynchronous-2.1.0 examples/async_patterns.rb
asynchronous-2.0.0 examples/async_patterns.rb