{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Copyright 2014 Brett Slatkin, Pearson Education Inc.\n", "#\n", "# Licensed under the Apache License, Version 2.0 (the \"License\");\n", "# you may not use this file except in compliance with the License.\n", "# You may obtain a copy of the License at\n", "#\n", "# http://www.apache.org/licenses/LICENSE-2.0\n", "#\n", "# Unless required by applicable law or agreed to in writing, software\n", "# distributed under the License is distributed on an \"AS IS\" BASIS,\n", "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", "# See the License for the specific language governing permissions and\n", "# limitations under the License.\n", "\n", "# Preamble to mimick book environment\n", "import logging\n", "from pprint import pprint\n", "from sys import stdout as STDOUT" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello from the child!\n", "\n" ] } ], "source": [ "# Example 1\n", "import subprocess\n", "proc = subprocess.Popen(\n", " ['echo', 'Hello from the child!'],\n", " stdout=subprocess.PIPE)\n", "out, err = proc.communicate()\n", "print(out.decode('utf-8'))" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Working...\n", "Working...\n", "Exit status 0\n" ] } ], "source": [ "# Example 2\n", "from time import sleep, time\n", "proc = subprocess.Popen(['sleep', '0.3'])\n", "while proc.poll() is None:\n", " print('Working...')\n", " # Some time consuming work here\n", " sleep(0.2)\n", "\n", "print('Exit status', proc.poll())" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Example 3\n", "def run_sleep(period):\n", " proc = subprocess.Popen(['sleep', str(period)])\n", " return proc\n", "\n", "start = time()\n", "procs = []\n", "for _ in range(10):\n", " proc = run_sleep(0.1)\n", " procs.append(proc)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Finished in 0.167 seconds\n" ] } ], "source": [ "# Example 4\n", "for proc in procs:\n", " proc.communicate()\n", "end = time()\n", "print('Finished in %.3f seconds' % (end - start))" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Example 5\n", "import os\n", "\n", "def run_openssl(data):\n", " env = os.environ.copy()\n", " env['password'] = b'\\xe24U\\n\\xd0Ql3S\\x11'\n", " proc = subprocess.Popen(\n", " ['openssl', 'enc', '-des3', '-pass', 'env:password'],\n", " env=env,\n", " stdin=subprocess.PIPE,\n", " stdout=subprocess.PIPE)\n", " proc.stdin.write(data)\n", " proc.stdin.flush() # Ensure the child gets input\n", " return proc" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Example 6\n", "import os\n", "procs = []\n", "for _ in range(3):\n", " data = os.urandom(10)\n", " proc = run_openssl(data)\n", " procs.append(proc)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "b'\\xc2\\xe1S~\\xab9.s9\\x16'\n", "b'\\x8a\\xfb\\xb4\\x90\\xbb<\\xf5\\xdd\\xf4\\x8c'\n", "b'b\\x8b9\\x9a4\\xe1\\x91t\\xe22'\n" ] } ], "source": [ "# Example 7\n", "for proc in procs:\n", " out, err = proc.communicate()\n", " print(out[-10:])" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Example 8\n", "def run_md5(input_stdin):\n", " proc = subprocess.Popen(\n", " ['md5'],\n", " stdin=input_stdin,\n", " stdout=subprocess.PIPE)\n", " return proc" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Example 9\n", "input_procs = []\n", "hash_procs = []\n", "for _ in range(3):\n", " data = os.urandom(10)\n", " proc = run_openssl(data)\n", " input_procs.append(proc)\n", " hash_proc = run_md5(proc.stdout)\n", " hash_procs.append(hash_proc)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "b'c2a92f44b5a5ce1163f0ab8363fc5c0a'\n", "b'32fd37d79de4df5c95d3626741b62f04'\n", "b'7b00b64a5b5348f9f32e17eb6549d6bc'\n" ] } ], "source": [ "# Example 10\n", "for proc in input_procs:\n", " proc.communicate()\n", "for proc in hash_procs:\n", " out, err = proc.communicate()\n", " print(out.strip())" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Exit status -15\n" ] } ], "source": [ "# Example 11\n", "proc = run_sleep(10)\n", "try:\n", " proc.communicate(timeout=0.1)\n", "except subprocess.TimeoutExpired:\n", " proc.terminate()\n", " proc.wait()\n", "\n", "print('Exit status', proc.poll())" ] } ], "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.5.1" } }, "nbformat": 4, "nbformat_minor": 0 }