सार्वजनिक जानकारी से बना चयन मार्गदर्शन। केवल संदर्भ हेतु।
Squadron एक Dart/Flutter पैकेज है जो आइसोलेट और वेब वर्कर पूल के माध्यम से बहु-थ्रेडिंग की अनुमति देता है, जिससे मुख्य धारा से CPU-तीव्र और भारी I/O संचालन को निकाला जा सकता है। यह Android, iOS, Web, macOS, Windows और Linux जैसे प्लेटफ़ॉर्मों पर समानांतर निष्पादन को सरल बनाता है। मूल प्लेटफ़ॉर्मों पर आइसोलेट और वेब पर वेब वर्कर का उपयोग करके, Squadron ऐप की प्रतिक्रिया शक्ति और प्रदर्शन में सुधार करता है। इस पैकेज में कार्यक्रम धाराओं के प्रबंधन और कार्य वितरण के लिए उपयोग में आसान API प्रदान करता है।
उपयोग
CPU-भारी गणना को हटाना
बड़े फ़ाइल I/O संचालन का प्रबंधन
तीव्र कार्यों के दौरान UI प्रतिक्रियाशीलता में सुधार करना
बैकग्राउंड डेटा प्रोसेसिंग चल रही है
मुख्य धागे को ब्लॉक किए बिना लंबे समय तक चलने वाले एल्गोरिदम को निष्पादित करना
मुख्य विशेषताएँ
Isolate और Web Worker पूल का समर्थन करता है
क्रॉस-प्लेटफॉर्म संगतता
सरल कार्य सबमिशन API
स्वचालित कार्य प्रबंधन
निर्मित त्रुटि संभाल
थ्रेड-सुरक्षित संचार
इनके लिए उपयुक्त
उच्च प्रदर्शन बैकग्राउंड प्रोसेसिंग की आवश्यकता वाले विकासकर्ता
जटिल डेटा परिवर्तन वाले ऐप्स
मीडिया या छवि प्रोसेसिंग ऐप्स
रियल-टाइम डेटा विश्लेषण उपकरण
भारी लोड के दौरान प्रतिक्रियाशील UI की आवश्यकता वाले अनुप्रयोग
विचार योग्य बातें
आइसोलेट और कॉन्करेंसी के बारे में समझ की आवश्यकता है
बहुत सारे वर्कर के साथ मेमोरी उपयोग बढ़ सकता है
ब्राउज़र क्षमताओं द्वारा सीमित वेब समर्थन
हल्के कार्यों के लिए उपयुक्त नहीं
थ्रेड्स के बीच सावधानीपूर्वक राज्य प्रबंधन की आवश्यकता होती है
मूल README
संबंधित सोर्स रिपॉज़िटरी के मूल README का स्नैपशॉट। नवीनतम सामग्री के लिए रिपॉज़िटरी देखें।
Squadron - Multithreading and worker pools in Dart
Offload CPU-bound and long running tasks and give your apps some air!
Works everywhere: Desktop, Server, Mobile, and Browser (JS & WASM).
Dart is single-threaded by nature. While Isolate.run() is great for one-off tasks, it creates overhead for frequent operations, and is not available on browser platforms.
Squadron provides:
🔄 Persistent Worker Pools: Keep workers alive and ready to process tasks instantly.
🌐 Seamless Web Support: Uniform API for Isolates (Native) and Web Workers (JS/Wasm).
🛡️ Type Safety: Automatic code generation ensures your thread boundaries are type-safe.
📡 Streaming: Native support for Stream return types from workers.
🏗️ Architecture
sequenceDiagram
autonumber
participant App as Main App (UI)
box rgb(40, 100, 200, 0.1) Squadron / Generated Code
participant Pool as HelloWorldWorkerPool/Worker
end
box rgb(0, 150, 0, 0.1) Background Thread
participant Service as User Service
end
App->>Pool: worker.hello('Squadron')
Pool->>Service: Assign Task (Marshaled)
activate Service
Note over Service: Executing on background Isolate/Web Worker
Service-->>Pool: Return Result (Unmarshaled)
deactivate Service
Pool-->>App: Future<String> Result
Squadron uses code generation to handle the boilerplate of thread communication. Run the following command:
dart run build_runner build
This creates HelloWorldWorker and HelloWorldWorkerPool. These classes implement the same interface as your service but proxy calls to background threads.
4. Running your Worker
In your application, instantiate the worker and use it like a normal service.
[!IMPORTANT] Always stop your workers. Failure to call worker.stop() will keep your program running indefinitely.
import'hello_world.dart';
voidmain() async {
final worker = HelloWorldWorker();
try {
// Squadron starts the worker automatically on the first callfinal message = await worker.hello('Squadron');
print(message);
} finally {
worker.stop(); // Clean up the background thread
}
}
🌍 Web Support (JS & Wasm)
Squadron is designed for a "write once, run anywhere" experience. When targeting the web, you must compile your worker entry points:
# Compile to JavaScript
dart compile js ".\src\lib\hello_world.web.g.dart" -o "..\web\workers\hello_world.web.g.dart.js"# Compile to Web Assembly
dart compile wasm ".\src\lib\hello_world.web.g.dart" -o "..\web\workers\hello_world.web.g.dart.wasm"
Squadron will automatically detect the runtime environment and choose the correct implementation.
🛠️ Advanced Concepts
Data Transfer & Marshaling
Because workers run in separate memory spaces, data must be serialized ("marshaled") to cross thread boundaries. While base types (Strings, Numbers, Lists, Maps) are handled automatically, custom objects require a SquadronMarshaler.
Exceptions thrown in a worker are caught, serialized, and re-thrown on the caller's side, preserving the stack trace and error information where possible.
Debugging background workers can be tricky. Squadron provides mechanisms to forward log messages from workers back to the main thread's debugger or console.