{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Multi-Processing-Beispiel\n", "\n", "Wir beginnen hier mit Code, der klar und einfach ist und von oben nach unten ausgeführt wird. Er ist einfach zu entwickeln und inkrementell zu testen:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "execution": { "iopub.execute_input": "2026-05-21T21:41:20.651686Z", "iopub.status.busy": "2026-05-21T21:41:20.651442Z", "iopub.status.idle": "2026-05-21T21:41:21.754611Z", "shell.execute_reply": "2026-05-21T21:41:21.754059Z", "shell.execute_reply.started": "2026-05-21T21:41:20.651669Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "('https://cusy.io/en', 50276)\n", "('https://pyviz-tutorial.readthedocs.io/de/latest/', 37736)\n", "('https://jupyter-tutorial.readthedocs.io/en/latest/', 42963)\n", "('https://github.com/veit/jupyter-tutorial/', 319332)\n", "('https://github.com/veit/pyviz-tutorial/', 285369)\n" ] } ], "source": [ "from multiprocessing.pool import ThreadPool as Pool\n", "\n", "import requests\n", "\n", "\n", "sites = [\n", " \"https://github.com/veit/jupyter-tutorial/\",\n", " \"https://jupyter-tutorial.readthedocs.io/en/latest/\",\n", " \"https://github.com/veit/pyviz-tutorial/\",\n", " \"https://pyviz-tutorial.readthedocs.io/de/latest/\",\n", " \"https://cusy.io/en\",\n", "]\n", "\n", "\n", "def sitesize(url):\n", " with requests.get(url, timeout=10) as u:\n", " return url, len(u.content)\n", "\n", "\n", "pool = Pool(10)\n", "for result in pool.imap_unordered(sitesize, sites):\n", " print(result)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "