{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# subprocess.Popen" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Writing date1.py\n" ] } ], "source": [ "%%writefile date1.py\n", "\n", "import subprocess\n", "\n", "subprocess.Popen('date')" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Sun May 17 01:25:15 YEKT 2015\r\n" ] } ], "source": [ "!python3.4 date1.py" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Writing date2.py\n" ] } ], "source": [ "%%writefile date2.py\n", "\n", "import subprocess\n", "\n", "subprocess.Popen(['date', '+%H:%M:%S'])" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "01:25:17\r\n" ] } ], "source": [ "!python3.4 date2.py" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Writing date3.py\n" ] } ], "source": [ "%%writefile date3.py\n", "\n", "import subprocess\n", "\n", "subprocess.Popen('date +%H:%M:%S', shell=True)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "01:25:20\r\n" ] } ], "source": [ "!python3.4 date3.py" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Writing date4.py\n" ] } ], "source": [ "%%writefile date4.py\n", "\n", "import subprocess\n", "\n", "subprocess.Popen('date', stdout=subprocess.DEVNULL)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": true }, "outputs": [], "source": [ "!python3.4 date4.py" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "None\n", "b'Hello!\\n'\n", "0\n" ] } ], "source": [ "import subprocess\n", "\n", "p = subprocess.Popen('cat', stdin=subprocess.PIPE, stdout=subprocess.PIPE)\n", "p.stdin.write(b'Hello!\\n')\n", "p.stdin.close()\n", "print(p.poll())\n", "print(p.stdout.read())\n", "print(p.poll())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# fork" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Writing fork.py\n" ] } ], "source": [ "%%writefile fork.py\n", "\n", "import os\n", "\n", "pid = os.fork()\n", "if pid:\n", " # parent\n", " for _ in range(200):\n", " print('hello from parent')\n", "else:\n", " # child\n", " for _ in range(200):\n", " print('hello from child')" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from child\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from child\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from child\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from child\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from child\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from child\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from child\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from child\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from child\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from child\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from parent\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from parent\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n", "hello from child\r\n" ] } ], "source": [ "!python3.4 fork.py" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# threading" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## def" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Writing threads1.py\n" ] } ], "source": [ "%%writefile threads1.py\n", "\n", "import threading\n", "\n", "def action():\n", " for _ in range(10):\n", " print('hello from', threading.current_thread().name)\n", "\n", "for _ in range(10):\n", " thread = threading.Thread(target=action)\n", " thread.start()" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hello from Thread-1\r\n", "hello from Thread-1\r\n", "hello from Thread-1\r\n", "hello from Thread-1\r\n", "hello from Thread-1\r\n", "hello from Thread-1\r\n", "hello from Thread-2\r\n", "hello from Thread-2\r\n", "hello from Thread-2\r\n", "hello from Thread-2\r\n", "hello from Thread-2\r\n", "hello from Thread-2\r\n", "hello from Thread-1\r\n", "hello from Thread-3\r\n", "hello from Thread-2\r\n", "hello from Thread-1\r\n", "hello from Thread-5\r\n", "hello from Thread-4\r\n", "hello from Thread-7\r\n", "hello from Thread-1\r\n", "hello from Thread-3\r\n", "hello from Thread-2\r\n", "hello from Thread-4\r\n", "hello from Thread-4\r\n", "hello from Thread-8\r\n", "hello from Thread-5\r\n", "hello from Thread-9\r\n", "hello from Thread-6\r\n", "hello from Thread-1\r\n", "hello from Thread-10\r\n", "hello from Thread-3\r\n", "hello from Thread-2\r\n", "hello from Thread-7\r\n", "hello from Thread-4\r\n", "hello from Thread-8\r\n", "hello from Thread-3\r\n", "hello from Thread-9\r\n", "hello from Thread-7\r\n", "hello from Thread-4\r\n", "hello from Thread-8\r\n", "hello from Thread-3\r\n", "hello from Thread-9\r\n", "hello from Thread-7\r\n", "hello from Thread-4\r\n", "hello from Thread-8\r\n", "hello from Thread-3\r\n", "hello from Thread-9\r\n", "hello from Thread-7\r\n", "hello from Thread-4\r\n", "hello from Thread-4\r\n", "hello from Thread-10\r\n", "hello from Thread-9\r\n", "hello from Thread-7\r\n", "hello from Thread-8\r\n", "hello from Thread-4\r\n", "hello from Thread-5\r\n", "hello from Thread-9\r\n", "hello from Thread-7\r\n", "hello from Thread-8\r\n", "hello from Thread-4\r\n", "hello from Thread-5\r\n", "hello from Thread-6\r\n", "hello from Thread-7\r\n", "hello from Thread-10\r\n", "hello from Thread-2\r\n", "hello from Thread-5\r\n", "hello from Thread-6\r\n", "hello from Thread-7\r\n", "hello from Thread-9\r\n", "hello from Thread-5\r\n", "hello from Thread-6\r\n", "hello from Thread-7\r\n", "hello from Thread-9\r\n", "hello from Thread-5\r\n", "hello from Thread-5\r\n", "hello from Thread-8\r\n", "hello from Thread-6\r\n", "hello from Thread-5\r\n", "hello from Thread-9\r\n", "hello from Thread-3\r\n", "hello from Thread-8\r\n", "hello from Thread-10\r\n", "hello from Thread-6\r\n", "hello from Thread-5\r\n", "hello from Thread-9\r\n", "hello from Thread-3\r\n", "hello from Thread-8\r\n", "hello from Thread-6\r\n", "hello from Thread-10\r\n", "hello from Thread-3\r\n", "hello from Thread-8\r\n", "hello from Thread-6\r\n", "hello from Thread-10\r\n", "hello from Thread-6\r\n", "hello from Thread-10\r\n", "hello from Thread-6\r\n", "hello from Thread-10\r\n", "hello from Thread-3\r\n", "hello from Thread-10\r\n", "hello from Thread-10\r\n" ] } ], "source": [ "!python3.4 threads1.py" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## class" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Writing threads2.py\n" ] } ], "source": [ "%%writefile threads2.py\n", "\n", "import threading\n", "\n", "class Worker(threading.Thread):\n", " def run(self):\n", " for _ in range(10):\n", " print('hello from', threading.current_thread().name)\n", "\n", "for _ in range(10):\n", " thread = Worker()\n", " thread.start()" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hello from Thread-1\r\n", "hello from Thread-1\r\n", "hello from Thread-1\r\n", "hello from Thread-1\r\n", "hello from Thread-1\r\n", "hello from Thread-1\r\n", "hello from Thread-1\r\n", "hello from Thread-1\r\n", "hello from Thread-1\r\n", "hello from Thread-1\r\n", "hello from Thread-2\r\n", "hello from Thread-2\r\n", "hello from Thread-2\r\n", "hello from Thread-2\r\n", "hello from Thread-2\r\n", "hello from Thread-2\r\n", "hello from Thread-2\r\n", "hello from Thread-2\r\n", "hello from Thread-2\r\n", "hello from Thread-2\r\n", "hello from Thread-3\r\n", "hello from Thread-3\r\n", "hello from Thread-3\r\n", "hello from Thread-3\r\n", "hello from Thread-3\r\n", "hello from Thread-3\r\n", "hello from Thread-3\r\n", "hello from Thread-3\r\n", "hello from Thread-3\r\n", "hello from Thread-3\r\n", "hello from Thread-4\r\n", "hello from Thread-4\r\n", "hello from Thread-4\r\n", "hello from Thread-4\r\n", "hello from Thread-4\r\n", "hello from Thread-4\r\n", "hello from Thread-4\r\n", "hello from Thread-4\r\n", "hello from Thread-4\r\n", "hello from Thread-4\r\n", "hello from Thread-5\r\n", "hello from Thread-5\r\n", "hello from Thread-5\r\n", "hello from Thread-5\r\n", "hello from Thread-5\r\n", "hello from Thread-5\r\n", "hello from Thread-5\r\n", "hello from Thread-5\r\n", "hello from Thread-5\r\n", "hello from Thread-5\r\n", "hello from Thread-6\r\n", "hello from Thread-6\r\n", "hello from Thread-6\r\n", "hello from Thread-6\r\n", "hello from Thread-6\r\n", "hello from Thread-6\r\n", "hello from Thread-6\r\n", "hello from Thread-6\r\n", "hello from Thread-6\r\n", "hello from Thread-6\r\n", "hello from Thread-7\r\n", "hello from Thread-7\r\n", "hello from Thread-7\r\n", "hello from Thread-7\r\n", "hello from Thread-7\r\n", "hello from Thread-7\r\n", "hello from Thread-7\r\n", "hello from Thread-7\r\n", "hello from Thread-7\r\n", "hello from Thread-7\r\n", "hello from Thread-8\r\n", "hello from Thread-8\r\n", "hello from Thread-8\r\n", "hello from Thread-8\r\n", "hello from Thread-8\r\n", "hello from Thread-8\r\n", "hello from Thread-8\r\n", "hello from Thread-8\r\n", "hello from Thread-8\r\n", "hello from Thread-8\r\n", "hello from Thread-9\r\n", "hello from Thread-9\r\n", "hello from Thread-9\r\n", "hello from Thread-9\r\n", "hello from Thread-9\r\n", "hello from Thread-9\r\n", "hello from Thread-9\r\n", "hello from Thread-9\r\n", "hello from Thread-9\r\n", "hello from Thread-9\r\n", "hello from Thread-10\r\n", "hello from Thread-10\r\n", "hello from Thread-10\r\n", "hello from Thread-10\r\n", "hello from Thread-10\r\n", "hello from Thread-10\r\n", "hello from Thread-10\r\n", "hello from Thread-10\r\n", "hello from Thread-10\r\n", "hello from Thread-10\r\n" ] } ], "source": [ "!python3.4 threads2.py" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Неатомарный инкремент в препарированном виде" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "152\n" ] } ], "source": [ "import threading\n", "import time\n", "\n", "counter = 0\n", "\n", "def inc():\n", " global counter\n", " v = counter\n", " time.sleep(0.001)\n", " counter = v + 1\n", "\n", "pool = []\n", "for _ in range(1000):\n", " th = threading.Thread(target=inc)\n", " th.start()\n", " pool.append(th)\n", "\n", "for th in pool:\n", " th.join()\n", "\n", "print(counter)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Неатомарный инкремент" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "903724\n" ] } ], "source": [ "import threading\n", "\n", "counter = 0\n", "\n", "def inc():\n", " global counter\n", " for _ in range(100000):\n", " counter += 1\n", "\n", "pool = []\n", "for _ in range(10):\n", " th = threading.Thread(target=inc)\n", " th.start()\n", " pool.append(th)\n", "\n", "for th in pool:\n", " th.join()\n", "\n", "print(counter)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Синхронизация потоков при помощи блокировки" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1000\n" ] } ], "source": [ "import threading\n", "import time\n", "\n", "counter = 0\n", "lock = threading.Lock()\n", "\n", "def inc():\n", " global counter\n", " lock.acquire()\n", " v = counter\n", " time.sleep(0.001)\n", " counter = v + 1\n", " lock.release()\n", "\n", "pool = []\n", "for _ in range(1000):\n", " th = threading.Thread(target=inc)\n", " th.start()\n", " pool.append(th)\n", "\n", "for th in pool:\n", " th.join()\n", "\n", "print(counter)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Использование with вместе с lock" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1000\n" ] } ], "source": [ "import threading\n", "import time\n", "\n", "counter = 0\n", "lock = threading.Lock()\n", "\n", "def inc():\n", " global counter\n", " with lock:\n", " v = counter\n", " time.sleep(0.001)\n", " counter = v + 1\n", "\n", "pool = []\n", "for _ in range(1000):\n", " th = threading.Thread(target=inc)\n", " th.start()\n", " pool.append(th)\n", "\n", "for th in pool:\n", " th.join()\n", "\n", "print(counter)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Взаимные блокировки, deadlock" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "locking failed :(\n", "locking failed :(\n" ] } ], "source": [ "import threading\n", "import time\n", "\n", "lock1 = threading.Lock()\n", "lock2 = threading.Lock()\n", "\n", "def good():\n", " with lock1:\n", " time.sleep(0.001)\n", " if lock2.acquire(timeout=1):\n", " print('good')\n", " lock2.release()\n", " else:\n", " print('locking failed :(')\n", "\n", "def bad():\n", " with lock2:\n", " if lock1.acquire(timeout=1):\n", " print('bad')\n", " lock1.release()\n", " else:\n", " print('locking failed :(')\n", "\n", "threading.Thread(target=good).start()\n", "threading.Thread(target=bad).start()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.4.3" } }, "nbformat": 4, "nbformat_minor": 0 }