Parallel Computations
Powerful Parallel Computing with BoxLang's Async Framework
Introduced in v1.4.0
BoxLang's async framework provides powerful tools for parallel computing, allowing you to execute multiple operations concurrently and efficiently process large datasets. Whether you're processing arrays, transforming data structures, or running independent computations, BoxLang's parallel computing BIFs give you the tools to maximize performance while maintaining code simplicity.
Sequential: Task1 β Task2 β Task3 β Task4 (16 seconds)
π¦ π¦ π¦ π¦
Parallel: Task1 β
Task2 β Results (4 seconds)
Task3 β
Task4 β
π¦π¦π¦π¦π― Core Parallel Computing BIFs
BoxLang provides several Built-In Functions (BIFs) for parallel computing:
asyncAll()
Execute futures in parallel
Array of futures/functions
BoxFuture
asyncAny()
Race multiple futures
Array of futures/functions
BoxFuture
asyncRun()
Execute single function async
Function
BoxFuture
asyncAllApply()
Apply function to collection in parallel
Array/Struct + mapper
Array/Struct
futureNew()
Create new BoxFuture
Value/Function
BoxFuture
π Basic Parallel Execution
asyncAll() - Execute All in Parallel
The asyncAll() BIF executes multiple operations concurrently and returns results in order:
Advanced Example with Mixed Types:
asyncAny() - Race to the Finish
The asyncAny() BIF returns the result of the first operation to complete:
Timeout and Fallback Pattern:
π Data Processing with asyncAllApply()
The asyncAllApply() BIF is perfect for applying transformations to collections in parallel:
Array Processing
Struct Processing
With Error Handling
With Custom Executor and Timeout
π’ Collection Parallel Methods
BoxLang provides powerful parallel processing capabilities for all collection types (arrays, lists, queries, and structs) through parallel versions of common iteration methods. These methods can dramatically improve performance when processing large collections or when the callback functions involve expensive operations.
Just make sure you understand the intricacies of creating and managing parallel tasks, as improper use can lead to resource contention or inefficient execution.
π Supported Collection Types
All parallel methods work across these data types:
Array
arrayEach(), arrayEvery(), arrayFilter(), arrayMap(), arrayNone(), arraySome()
List
listEach(), listEvery(), listFilter(), listMap(), listNone(), listSome()
Query
queryEach(), queryEvery(), queryFilter(), queryMap(), queryNone(), querySome()
Struct
structEach(), structEvery(), structFilter(), structMap(), structNone(), structSome()
π― Collection Method Purposes
Each collection method serves a specific purpose in data processing:
Each
Iteration with side effects
Collection + callback
void
Logging, notifications, updating external state
Every
Universal validation
Collection + predicate
boolean
Check if ALL items meet the criteria predicate
Filter
Selective extraction
Collection + predicate
New collection
Extract items matching conditions
Map
Transformation
Collection + mapper
New collection
Transform each item to new form
None
Negative validation
Collection + predicate
boolean
Ensure NO items meet the criteria predicate
Some
Existence check
Collection + predicate
boolean
Check if ANY items meet the criteria predicate
π Each - Perform Actions
Execute side effects for each item without returning results:
β Every - Validate All
Test whether ALL items in a collection meet a condition:
π Filter - Extract Matching Items
Create a new collection containing only items that meet criteria:
π Map - Transform Items
Transform each item into a new form, creating a new collection:
β None - Ensure Absence
Verify that NO items in the collection meet a condition:
π Some - Check Existence
Test whether AT LEAST ONE item meets a condition:
ποΈ Parallel Execution Parameters
All collection parallel methods support these consistent parameters:
Execution Modes:
parallel: false- Sequential execution (default)parallel: true, maxThreads: null- Uses common ForkJoinPool (automatic thread count)parallel: true, maxThreads: N- Creates custom ForkJoinPool with N threadsparallel: true, virtual: true- Uses virtual threads for unlimited concurrencyparallel: true, maxThreads: true- Shorthand for virtual threads (positional syntax)
π Array Parallel Processing
π List Parallel Processing
List methods include additional delimiter parameters before parallel options:
ποΈ Query Parallel Processing
ποΈ Struct Parallel Processing
π― Method-Specific Examples
Each - Side Effects
Every - Validation
Some - Existence Check
None - Negative Validation
β‘ Performance Benefits
β‘ Single Async Operations
asyncRun() - Basic Async Execution
With Custom Executor:
futureNew() - Creating Futures
π Performance Monitoring
Timing Parallel Operations
Memory and Resource Monitoring
π Related Documentation
Async Pipelines - For chaining asynchronous operations
Executors - For managing thread pools and execution
Scheduled Tasks - For time-based parallel execution
π Performance Comparison
With BoxLang's parallel computing capabilities, you can dramatically improve application performance by leveraging multiple CPU cores and concurrent execution. Choose the right BIF for your use case, handle errors properly, and always consider resource management for optimal results.
Last updated
Was this helpful?
