{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os \n",
    "import multiprocessing as mp\n",
    "import numpy as np\n",
    "import time\n",
    "import math\n",
    "from joblib import Parallel, delayed\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def cpu_intensive_task(duration=5):\n",
    "    \"\"\"\n",
    "    A CPU-intensive function that runs for approximately the specified duration.\n",
    "    \n",
    "    :param duration: The approximate time in seconds the function should run\n",
    "    :return: The number of iterations completed\n",
    "    \"\"\"\n",
    "    start_time = time.time()\n",
    "    iterations = 0\n",
    "    \n",
    "    while time.time() - start_time < duration:\n",
    "        # Perform some arbitrary CPU-intensive calculations\n",
    "        for i in range(100000):\n",
    "            math.sqrt(i) * math.sin(i) * math.cos(i)\n",
    "        iterations += 1\n",
    "    \n",
    "    return iterations\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# create items\n",
    "items = np.arange(1000)\n",
    "\n",
    "with mp.Pool(processes=100) as pool:\n",
    "    results = pool.map(cpu_intensive_task, items)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "results = Parallel(n_jobs=100)(delayed(cpu_intensive_task)(item) for item in items)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "suno_env",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
