Worker Threads (কাজ কর্মী)
True parallel processing with worker threads for CPU-intensive tasks, enabling multi-core utilization in BanglaCode.
Overview
Worker Threads enable true parallelism in BanglaCode by running code in separate threads. Unlike async/await which handles concurrency, workers leverage multiple CPU cores for CPU-bound tasks:
- Parallel execution: Run multiple tasks simultaneously on different CPU cores
- Non-blocking: Workers run independently without blocking the main thread
- Message passing: Communicate via messages (no shared memory)
- Isolated state: Each worker has its own environment and variables
⚡ Use Cases: Image processing, data analysis, cryptography, large file parsing, mathematical computations, video encoding, or any CPU-intensive task that would block the main thread.
Quick Start
// Create a worker
dhoro worker = kaj_kormi_srishti(kaj() {
dekho("Worker running on separate thread!");
// Perform CPU-intensive task
dhoro sum = 0;
ghuriye (dhoro i = 0; i < 1000000; i = i + 1) {
sum = sum + i;
}
dekho("Computation complete:", sum);
});
// Send messages to worker
kaj_kormi_pathao(worker, "Process this data");
// Terminate when done
process_ghum(2000);
kaj_kormi_bondho(worker);API Reference
kaj_kormi_srishti(fn, data?)
কাজ কর্মী সৃষ্টি - Create worker
Creates a new worker thread that executes the given function in parallel.
// Basic worker
dhoro worker = kaj_kormi_srishti(kaj() {
dekho("Worker started");
});
// Worker with initial data
dhoro worker = kaj_kormi_srishti(kaj() {
dhoro config = kaj_kormi_tothya;
dekho("Config:", config);
}, {"threads": 4, "mode": "fast"});fn(Function) - Function to execute in worker threaddata(Any, optional) - Initial data accessible viakaj_kormi_tothya
kaj_kormi_pathao(worker, message)
কাজ কর্মী পাঠাও - Send to worker
Sends a message to the worker thread. Worker can receive via message handlers.
kaj_kormi_pathao(worker, "START");
kaj_kormi_pathao(worker, {"action": "process", "data": [1, 2, 3]});
kaj_kormi_pathao(worker, 42);worker(Worker) - Target workermessage(Any) - Message to send (string, number, object, array)
kaj_kormi_bondho(worker)
কাজ কর্মী বন্ধ - Stop worker
Terminates the worker thread immediately. Worker will stop execution and clean up resources.
kaj_kormi_bondho(worker); // Stop worker immediatelyworker(Worker) - Worker to terminate
kaj_kormi_shuno(worker, callback)
কাজ কর্মী শুনো - Listen to worker
Sets up a listener for messages from the worker. Called when worker sends data back to parent.
kaj_kormi_shuno(worker, kaj(data) {
dekho("Worker sent:", data);
jodi (data == "DONE") {
kaj_kormi_bondho(worker);
}
});worker(Worker) - Worker to listen tocallback(Function) - Handler called with message data
kaj_kormi_tothya
কাজ কর্মী তথ্য - Worker data
Special variable accessible inside worker function containing initial data passed during worker creation.
dhoro worker = kaj_kormi_srishti(kaj() {
// Access initial data
dhoro config = kaj_kormi_tothya;
dekho("Processing with config:", config);
}, {"mode": "fast", "threads": 4});Real-World Examples
Example 1: Parallel Data Processing
Process large datasets in parallel by dividing work across multiple workers.
// Divide array processing across 4 workers
dhoro data = [];
ghuriye (dhoro i = 0; i < 1000; i = i + 1) {
data = dhaaka(data, i);
}
dhoro numWorkers = 4;
dhoro chunkSize = dorghyo(data) / numWorkers;
dhoro workers = [];
dhoro results = [];
// Create workers for each chunk
ghuriye (dhoro i = 0; i < numWorkers; i = i + 1) {
dhoro start = i * chunkSize;
dhoro end = start + chunkSize;
dhoro chunk = [];
ghuriye (dhoro j = start; j < end; j = j + 1) {
chunk = dhaaka(chunk, data[j]);
}
dhoro worker = kaj_kormi_srishti(kaj() {
dhoro chunk = kaj_kormi_tothya;
dhoro sum = 0;
// Process chunk
ghuriye (dhoro k = 0; k < dorghyo(chunk); k = k + 1) {
sum = sum + chunk[k] * chunk[k]; // Square each number
}
dekho("Worker", i, "completed. Sum:", sum);
}, chunk);
workers = dhaaka(workers, worker);
}
// Wait for all workers
process_ghum(2000);
// Clean up
ghuriye (dhoro i = 0; i < dorghyo(workers); i = i + 1) {
kaj_kormi_bondho(workers[i]);
}
dekho("All workers completed!");Example 2: CPU-Intensive Prime Calculation
Offload CPU-heavy computation to worker without blocking main thread.
// Main thread remains responsive
dekho("Starting prime calculation...");
dhoro primeWorker = kaj_kormi_srishti(kaj() {
// Check if number is prime
kaj isPrime(n) {
jodi (n <= 1) { ferao mittha; }
ghuriye (dhoro i = 2; i * i <= n; i = i + 1) {
jodi (n % i == 0) { ferao mittha; }
}
ferao sotti;
}
// Find all primes up to 10000
dhoro primes = [];
ghuriye (dhoro i = 2; i <= 10000; i = i + 1) {
jodi (isPrime(i)) {
primes = dhaaka(primes, i);
}
}
dekho("Found", dorghyo(primes), "primes");
}, khali);
// Main thread continues executing
dekho("Main thread still responsive!");
// Wait for worker to complete
process_ghum(3000);
kaj_kormi_bondho(primeWorker);Example 3: Batch Processing with Multiple Workers
Process multiple items in parallel with a worker pool pattern.
// Simulate processing multiple files in parallel
dhoro files = ["file1.txt", "file2.txt", "file3.txt", "file4.txt"];
dhoro workers = [];
dhoro completed = 0;
ghuriye (dhoro i = 0; i < dorghyo(files); i = i + 1) {
dhoro worker = kaj_kormi_srishti(kaj() {
dhoro filename = kaj_kormi_tothya;
dekho("Processing", filename);
// Simulate heavy processing
dhoro operations = 0;
ghuriye (dhoro j = 0; j < 1000000; j = j + 1) {
operations = operations + 1;
}
dekho("Completed", filename);
}, files[i]);
workers = dhaaka(workers, worker);
}
// Wait for all workers
process_ghum(3000);
// Terminate all workers
ghuriye (dhoro i = 0; i < dorghyo(workers); i = i + 1) {
kaj_kormi_bondho(workers[i]);
}
dekho("All files processed!");Best Practices
✅ DO:
- Use for CPU-bound tasks: Image processing, data analysis, cryptography
- Always terminate workers: Call
kaj_kormi_bondho()when done to free resources - Divide work efficiently: Split large tasks into chunks for parallel processing
- Limit worker count: Create workers based on CPU core count (typically 2-8 workers)
- Pass immutable data: Send copies of data to avoid race conditions
❌ DON'T:
- Don't use for I/O operations: Use async/await instead (file reading, network requests)
- Don't create too many workers: More workers than CPU cores causes overhead
- Don't share state: Workers have isolated environments - pass data via messages
- Don't forget cleanup: Unterminated workers consume memory and CPU
- Don't use for small tasks: Worker overhead can exceed computation time
Performance Considerations
⚡ Optimization Tips:
- Worker pool pattern: Reuse workers for multiple tasks instead of creating new ones
- Optimal worker count: Number of workers = CPU cores (check with
cpu_sonkha()) - Chunk size matters: Balance between parallelism and overhead (chunks too small = overhead, too large = less parallel)
- Measure overhead: Worker creation has cost - only use for tasks > 100ms
When to Use Workers vs Async:
- Heavy computations (math, crypto)
- Data processing (parsing, transforming)
- Image/video processing
- Tasks that block > 100ms
- Network requests (HTTP, WebSocket)
- File I/O (reading/writing)
- Database queries
- Any I/O-bound operation
Common Use Cases
Data Processing
Parse and transform large datasets (CSV, JSON) in parallel chunks for faster processing.
Cryptography
Hash generation, encryption/decryption operations that are CPU-intensive.
Mathematical Computation
Prime finding, matrix operations, statistical analysis running in parallel.
Batch Processing
Process multiple files, images, or documents simultaneously with worker pool.
Related Features
Summary
Worker Threads bring true parallel processing to BanglaCode, enabling efficient multi-core CPU utilization for compute-intensive tasks. Use workers for CPU-bound operations, async/await for I/O-bound operations.
Key takeaways: Create workers with kaj_kormi_srishti(), communicate via messages, always terminate with kaj_kormi_bondho(), and optimize worker count based on CPU cores.