help@rskworld.in +91 93305 39277
RSK World
  • Home
  • Development
    • Web Development
    • Mobile Apps
    • Software
    • Games
    • Project
  • Technologies
    • Data Science
    • AI Development
    • Cloud Development
    • Blockchain
    • Cyber Security
    • Dev Tools
    • Testing Tools
  • About
  • Contact

Theme Settings

Color Scheme
Display Options
Font Size
100%
Back to Project
RSK World
dask-parallel
/
notebooks
RSK World
dask-parallel
Parallel and distributed computing with Dask
notebooks
  • 01_dask_arrays.ipynb4.2 KB
  • 02_dask_dataframes.ipynb5 KB
  • 03_delayed_computations.ipynb5.2 KB
  • 04_distributed_computing.ipynb4.8 KB
  • 05_task_scheduling.ipynb5.4 KB
  • 06_dask_bags.ipynb5.3 KB
  • 07_advanced_dataframes.ipynb6.7 KB
  • 08_dask_ml.ipynb7.2 KB
03_delayed_computations.ipynb
notebooks/03_delayed_computations.ipynb
Raw Download
Find: Go to:
{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Delayed Computations - Lazy Evaluation with Dask\n",
        "\n",
        "<!--\n",
        "Project: Dask Parallel Computing\n",
        "Author: Molla Samser\n",
        "Designer & Tester: Rima Khatun\n",
        "Website: https://rskworld.in\n",
        "Email: help@rskworld.in, support@rskworld.in\n",
        "Phone: +91 93305 39277\n",
        "-->\n",
        "\n",
        "This notebook demonstrates delayed computations and lazy evaluation in Dask.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from dask import delayed, compute\n",
        "import time\n",
        "import numpy as np\n",
        "\n",
        "print(\"Delayed Computations Demo\")\n",
        "print(\"=\" * 50)\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Basic Delayed Functions\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Define a function that takes time to execute\n",
        "@delayed\n",
        "def process_data(x):\n",
        "    \"\"\"Simulate data processing\"\"\"\n",
        "    time.sleep(0.1)  # Simulate work\n",
        "    return x * 2\n",
        "\n",
        "# Create delayed computations\n",
        "results = [process_data(i) for i in range(10)]\n",
        "\n",
        "print(\"Created delayed computations (not executed yet)\")\n",
        "print(f\"Type: {type(results[0])}\")\n",
        "\n",
        "# Now compute all results in parallel\n",
        "print(\"\\nComputing results in parallel...\")\n",
        "start_time = time.time()\n",
        "final_results = compute(*results)\n",
        "end_time = time.time()\n",
        "\n",
        "print(f\"Results: {final_results}\")\n",
        "print(f\"Total time: {end_time - start_time:.2f} seconds\")\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Complex Workflow\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "@delayed\n",
        "def load_data(filename):\n",
        "    \"\"\"Simulate loading data\"\"\"\n",
        "    time.sleep(0.2)\n",
        "    return np.random.rand(100)\n",
        "\n",
        "@delayed\n",
        "def process_data(data):\n",
        "    \"\"\"Simulate processing data\"\"\"\n",
        "    time.sleep(0.3)\n",
        "    return data.mean()\n",
        "\n",
        "@delayed\n",
        "def combine_results(*results):\n",
        "    \"\"\"Combine multiple results\"\"\"\n",
        "    time.sleep(0.1)\n",
        "    return sum(results)\n",
        "\n",
        "# Create workflow\n",
        "data1 = load_data('file1.csv')\n",
        "data2 = load_data('file2.csv')\n",
        "data3 = load_data('file3.csv')\n",
        "\n",
        "processed1 = process_data(data1)\n",
        "processed2 = process_data(data2)\n",
        "processed3 = process_data(data3)\n",
        "\n",
        "final_result = combine_results(processed1, processed2, processed3)\n",
        "\n",
        "print(\"Workflow created. Executing...\")\n",
        "start_time = time.time()\n",
        "result = final_result.compute()\n",
        "end_time = time.time()\n",
        "\n",
        "print(f\"Final result: {result}\")\n",
        "print(f\"Total time: {end_time - start_time:.2f} seconds\")\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Visualizing Task Graph\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Visualize the computation graph\n",
        "try:\n",
        "    final_result.visualize(filename='task_graph.png')\n",
        "    print(\"Task graph saved to task_graph.png\")\n",
        "except Exception as e:\n",
        "    print(f\"Visualization requires graphviz: {e}\")\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Parallel Processing Example\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "@delayed\n",
        "def expensive_computation(n):\n",
        "    \"\"\"Simulate expensive computation\"\"\"\n",
        "    time.sleep(0.5)\n",
        "    return sum(range(n))\n",
        "\n",
        "# Process multiple tasks in parallel\n",
        "tasks = [expensive_computation(i * 1000) for i in range(1, 11)]\n",
        "\n",
        "print(\"Processing 10 tasks in parallel...\")\n",
        "start_time = time.time()\n",
        "results = compute(*tasks)\n",
        "end_time = time.time()\n",
        "\n",
        "print(f\"Results: {results}\")\n",
        "print(f\"Total time: {end_time - start_time:.2f} seconds\")\n",
        "print(f\"\\nIf sequential, would take: {0.5 * 10:.2f} seconds\")\n"
      ]
    }
  ],
  "metadata": {
    "language_info": {
      "name": "python"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}
184 lines•5.2 KB
json

About RSK World

Founded by Molla Samser, with Designer & Tester Rima Khatun, RSK World is your one-stop destination for free programming resources, source code, and development tools.

Founder: Molla Samser
Designer & Tester: Rima Khatun

Development

  • Game Development
  • Web Development
  • Mobile Development
  • AI Development
  • Development Tools

Legal

  • Terms & Conditions
  • Privacy Policy
  • Disclaimer

Contact Info

Nutanhat, Mongolkote
Purba Burdwan, West Bengal
India, 713147

+91 93305 39277

hello@rskworld.in
support@rskworld.in

© 2026 RSK World. All rights reserved.

Content used for educational purposes only. View Disclaimer