parallel
Parallel
Crystal library for parallel processing using Fiber::ExecutionContext.
Requirements
- Crystal 1.6.0+
- Compile flags:
-Dpreview_mt -Dexecution_context
Installation
Add to shard.yml
:
dependencies:
parallel:
github: kojix2/parallel
Run shards install
Usage
require "parallel"
# Parallel map
[1, 2, 3, 4].par_map { |x| x * 2 }
# => [2, 4, 6, 8]
# Parallel each
[1, 2, 3, 4].par_each { |x| puts x }
# Chunk processing (fewer context switches)
[1, 2, 3, 4].par_map(chunk: 2) { |x| x * 2 }
# => [2, 4, 6, 8] (same result, better performance)
# Custom ExecutionContext
context = Fiber::ExecutionContext::MultiThreaded.new("workers", 8)
[1, 2, 3, 4].par_map(context) { |x| x * 2 }
Compilation
crystal build -Dpreview_mt -Dexecution_context your_app.cr
crystal spec -Dpreview_mt -Dexecution_context
Methods
-
par_map(execution_context = nil, *, chunk = nil, &block)
- Applies block to each element in parallel, returns array of results.
chunk
: Process elements in chunks to reduce context switches.
-
par_each(execution_context = nil, *, chunk = nil, &block)
- Applies block to each element in parallel for side effects.
chunk
: Process elements in chunks to reduce context switches.
Notes
- Works with any Enumerable (Array, Range, Set, Hash, etc.)
- Indexable types (Array, Slice) preserve order
- Exceptions are propagated from parallel tasks
- Uses global ExecutionContext by default for performance
- Thread safety is your responsibility when accessing shared resources
Contributing
- Fork this repository
- Report bugs
- Fix bugs and submit pull requests
- Write, clarify, or fix documentation
- Suggest or add new features
License
MIT
This library contains code generated by AI.
Repository
parallel
Owner
Statistic
- 0
- 0
- 0
- 0
- 0
- 4 days ago
- June 16, 2025
License
MIT License
Links
Synced at
Sat, 21 Jun 2025 09:52:58 GMT
Languages