parallel
parallel.cr
Parallel is a shard which provides tools for parallelism similar to how Crystal provides tools for concurrency. It provides an analogue of fibers and channels using processes, and a special type of channel to communicate between processes.
Installation
Add this to your application's shard.yml
:
dependencies:
parallel:
github: RX14/parallel.cr
Usage
In the following example, you would expect to complete the tasks in 3 seconds because it processes 4 jobs at a time. However, currently Crystal can only execute code on a single core at a time, and it will take the full 12 seconds to execute.
lib LibC
fun sleep(seconds : UInt)
end
def do_work(job)
LibC.sleep(1)
job * 2
end
def worker(in_channel, out_channel)
loop do
job = in_channel.receive
result = do_work(job)
out_channel.send(result)
end
end
job_channel = Channel(Int32).new
results_channel = Channel(Int32).new
# spawn 4 workers
4.times do
spawn worker(job_channel, results_channel)
end
spawn do
12.times { |i| job_channel.send i }
end
time = Time.measure do
12.times { p results_channel.receive }
end
p time
Simply by requiring "parallel"
, replacing spawn
with pspawn
, and Channel
with PChan
, this simple example will execute in only 3 seconds.
Development
TODO: Write development instructions here
Contributing
- Fork it ( https://github.com/RX14/parallel.cr/fork )
- Create your feature branch (git checkout -b my-new-feature)
- Commit your changes (git commit -am 'Add some feature')
- Push to the branch (git push origin my-new-feature)
- Create a new Pull Request
Contributors
- RX14 RX14 - creator, maintainer