{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Multi-processing example\n", "\n", "We’ll start with code that is clear, simple, and executed top-down. It’s easy to develop and incrementally testable:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "('https://cusy.io/en', 29771)\n", "('https://jupyter-tutorial.readthedocs.io/en/latest/', 43637)\n", "('https://pyviz-tutorial.readthedocs.io/de/latest/', 37252)\n", "('https://github.com/veit/jupyter-tutorial/', 366286)\n", "('https://github.com/veit/pyviz-tutorial/', 334712)\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) 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": [ "