Isolates এবং Message Passing এর ব্যবহার

Concurrency in Dart (কনকারেন্সি) - ডার্ট প্রোগ্রামিং (Dart Programming) - Computer Programming

314

Isolates এবং Message Passing এর ব্যবহার

Isolates এবং Message Passing হল Dart-এর কনকারেন্সি ব্যবস্থাপনার প্রধান কৌশল। Isolates একক থ্রেডে কাজ করে এবং তাদের মধ্যে যোগাযোগ করার জন্য Message Passing ব্যবহার করা হয়। এটি একটি নিরাপদ এবং কার্যকরী উপায়, যা ডেটা শেয়ারিং এড়িয়ে সিস্টেমের কার্যক্ষমতা বৃদ্ধি করে।

১. Isolates কী?

Isolates হল Dart-এ সম্পূর্ণ আলাদা এক্সিকিউশন থ্রেড। প্রতিটি isolate নিজস্ব মেমরি স্পেসে চলে এবং তারা একে অপরের মেমরিতে সরাসরি অ্যাক্সেস করতে পারে না। এর ফলে, একাধিক isolate একসাথে কাজ করতে পারে সুরক্ষিতভাবে।

Isolate তৈরি করার উদাহরণ:

import 'dart:isolate';

void sayHello(SendPort sendPort) {
  sendPort.send('Hello from the isolate!');
}

void main() async {
  // Creating a receive port to receive messages from the isolate
  ReceivePort receivePort = ReceivePort();

  // Spawning a new isolate
  Isolate.spawn(sayHello, receivePort.sendPort);

  // Listening for messages
  receivePort.listen((message) {
    print(message); // Output: Hello from the isolate!
    receivePort.close(); // Close the port when done
  });
}

২. Message Passing কী?

Message Passing হল isolates এর মধ্যে যোগাযোগ করার পদ্ধতি। যখন একটি isolate অন্য isolate-এর সাথে যোগাযোগ করতে চায়, তখন এটি একটি SendPort ব্যবহার করে একটি বার্তা পাঠায় এবং একটি ReceivePort ব্যবহার করে বার্তা গ্রহণ করে।

Message Passing এর উদাহরণ:

import 'dart:isolate';

// Function to run in the isolate
void isolateFunction(SendPort sendPort) {
  // Send a message back to the main isolate
  sendPort.send("Hello from the isolate!");
}

void main() async {
  // Create a receive port
  ReceivePort receivePort = ReceivePort();

  // Spawn an isolate and provide the SendPort
  Isolate.spawn(isolateFunction, receivePort.sendPort);

  // Listen for messages from the isolate
  receivePort.listen((message) {
    print(message); // Output: Hello from the isolate!
    receivePort.close(); // Close the port when done
  });
}

৩. Multiple Isolates

একাধিক isolate তৈরি করে এবং তাদের মধ্যে message passing ব্যবহার করে কিভাবে কাজ করতে হয় তা দেখুন।

import 'dart:isolate';

void worker(SendPort sendPort) {
  for (int i = 0; i < 5; i++) {
    sendPort.send("Message $i from the isolate.");
    // Simulate some work
    Future.delayed(Duration(milliseconds: 500));
  }
}

void main() async {
  // Create a receive port
  ReceivePort receivePort = ReceivePort();

  // Spawn an isolate
  Isolate.spawn(worker, receivePort.sendPort);

  // Listen for messages
  receivePort.listen((message) {
    print(message); // Output: Message 0 from the isolate, and so on
  });
}

৪. Advantages of Isolates and Message Passing

  • Safety: Isolates have their own memory, so there is no risk of shared state issues or race conditions.
  • Scalability: You can create multiple isolates to handle tasks concurrently, making your application scalable.
  • Performance: Isolates can take advantage of multiple cores, leading to performance improvements.

উপসংহার

Isolates এবং Message Passing Dart-এ কনকারেন্সি ব্যবস্থাপনার জন্য একটি শক্তিশালী উপায়। Isolates আলাদা থ্রেডে কার্যকরভাবে কাজ করে, এবং message passing ব্যবহার করে তারা নিরাপদভাবে যোগাযোগ করতে পারে। এই কৌশলগুলি ডেভেলপারদের জন্য দ্রুত, প্রতিক্রিয়াশীল এবং কার্যকর অ্যাপ্লিকেশন তৈরি করতে সহায়ক।

Content added By
Promotion

Are you sure to start over?

Loading...