diff --git a/17-futures/countries/Dockerfile b/17-futures/countries/Dockerfile new file mode 100644 index 0000000..4d40656 --- /dev/null +++ b/17-futures/countries/Dockerfile @@ -0,0 +1,41 @@ +# Use an official Python runtime as a base image +FROM centos + +# Set the working directory to /app +WORKDIR /app + +# Copy the current directory contents into the container at /app +ADD . /app + +#Unzip flag files +RUN yum install -y unzip +RUN unzip flags.zip + +#Install nginx +RUN yum install -y epel-release +RUN yum install -y nginx + +#Setup Nginx +RUN mv -f nginx.conf /etc/nginx/nginx.conf + +#Install vaurien +RUN yum install -y centos-release-scl gcc zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel python-devel python-pip +RUN pip install vaurien + + + + + + + +# Install any needed packages specified in requirements.txt +#RUN pip install -r requirements.txt + +# Make port 80 available to the world outside this container +#EXPOSE 80 + +# Define environment variable +#ENV NAME World + +# Run app.py when the container launches +#CMD ["python", "app.py"] diff --git a/17-futures/countries/README.rst b/17-futures/countries/README.rst index 3785ffd..b622bcb 100644 --- a/17-futures/countries/README.rst +++ b/17-futures/countries/README.rst @@ -20,175 +20,14 @@ Once these files are unpacked to the ``flags/`` directory and Nginx is configure Procedure ========= -1. Unpack test data -------------------- +1. Install Docker_. +.. _Docker: https://docs.docker.com/engine/installation/ +2. run: -The instructions in this section are for GNU/Linux or OSX using the command line. Windows users should have no difficulty doing the same operations with the Windows Exporer GUI. - -Unpack the initial data in the ``countries/`` directory:: - - $ unzip flags.zip - ... many lines omitted ... - creating: flags/zw/ - inflating: flags/zw/metadata.json - inflating: flags/zw/zw.gif - - -Verify that 194 directories are created in ``countries/flags/``, each with a ``.gif`` and a ``metadata.json`` file:: - - - $ ls flags | wc -w - 194 - $ find flags | grep .gif | wc -l - 194 - $ find flags | grep .json | wc -l - 194 - $ ls flags/ad - ad.gif metadata.json - - -2. Install Nginx ----------------- - -Download and install Nginx. I used version 1.6.2 -- the latest stable version as I write this. - -* Download page: http://nginx.org/en/download.html - -* Beginner's guide: http://nginx.org/en/docs/beginners_guide.html - - -3. Configure Nginx ------------------- - -Edit the the ``nginx.conf`` file to set the port and document root. You can determine which ``nginx.conf`` is in use by running:: - - - $ nginx -V - - -The output starts with:: - - nginx version: nginx/1.6.2 - built by clang 6.0 (clang-600.0.51) (based on LLVM 3.5svn) - TLS SNI support enabled - configure arguments:... - - -Among the configure arguments you'll see ``--conf-path=``. That's the file you will edit. - -Most of the content in ``nginx.conf`` is within a block labeled ``http`` and enclosed in curly braces. Within that block there can be multiple blocks labeled ``server``. Add another ``server`` block like this one:: - - - server { - listen 8001; - - location /flags/ { - root /full-path-to.../countries/; - } - } - - -After editing ``nginx.conf`` the server must be started (if it's not running) or told to reload the configuration file:: - - - $ nginx # to start, if necessary - $ nginx -s reload # to reload the configuration - - -To test the configuration, open the URL http://localhost:8001/flags/ad/ad.gif in a browser. You should see the blue, yellow and red flag of Andorra. - -If the test fails, please double check the procedure just described and refer to the Nginx documentation. - -At this point you may run the ``flags_*2.py`` examples against the Nginx install by providing the ``--server LOCAL`` command line option. For example:: - - - $ python3 flags2_threadpool.py -s LOCAL - LOCAL site: http://localhost:8001/flags - Searching for 20 flags: from BD to VN - 20 concurrent connections will be used. - -------------------- - 20 flags downloaded. - Elapsed time: 0.09s - - -Note that Nginx is so fast that you will not see much difference in run time between the sequential and the concurrent versions. For more realistic testing with simulated network lag, we need to set up the Vaurien proxy. - - -4. Install and run Vaurien --------------------------- - -Vaurien depends on gevent which is only available for Python 2.5-2.7. To install Vaurien I opened another shell, created a virtualenv for Python 2.7, and used that environment to install and run the ``vaurien`` package:: - - - $ virtualenv-2.7 .env27 --no-site-packages --distribute - New python executable in .env27/bin/python - Installing setuptools, pip...done. - $ . .env27/bin/activate - (.env27)$ pip install vaurien - Downloading/unpacking vaurien - Downloading vaurien-1.9.tar.gz (50kB): 50kB downloaded - ...many lines and a few minutes later... - - Successfully installed vaurien cornice gevent statsd-client vaurienclient - greenlet http-parser pyramid simplejson requests zope.interface - translationstring PasteDeploy WebOb repoze.lru zope.deprecation venusian - Cleaning up... - - -Using that same shell with the ``.env27`` environment active, run the ``vaurien_delay.sh`` script in the ``countries/`` directory:: - - - (.env27)$ $ ./vaurien_delay.sh - 2015-02-25 20:20:17 [69124] [INFO] Starting the Chaos TCP Server - 2015-02-25 20:20:17 [69124] [INFO] Options: - 2015-02-25 20:20:17 [69124] [INFO] * proxies from localhost:8002 to localhost:8001 - 2015-02-25 20:20:17 [69124] [INFO] * timeout: 30 - 2015-02-25 20:20:17 [69124] [INFO] * stay_connected: 0 - 2015-02-25 20:20:17 [69124] [INFO] * pool_max_size: 100 - 2015-02-25 20:20:17 [69124] [INFO] * pool_timeout: 30 - 2015-02-25 20:20:17 [69124] [INFO] * async_mode: 1 - - -The ``vaurien_delay.sh`` creates an HTTP proxy on port 8002 which adds a 1s delay to every response. You can test it with a browser on port 8002: http://localhost:8002/flags/ad/ad.gif -- the flag of Andorra should appear after 1 second. - -There is also the ``vaurien_error_delay.sh`` script which runs a proxy on port 8003 producing errors in 25% of the responses and a .5 s delay to 50% of the responses. You can also test it with the browser on port 8003, but rememeber that errors are expected. - - -Platform-specific instructions -============================== - - -Nginx setup on Mac OS X ------------------------- - -Homebrew (copy & paste code at the bottom of http://brew.sh/):: - - - $ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" - $ brew doctor - $ brew install nginx - - -Download and unpack:: - -Docroot is: /usr/local/var/www -/usr/local/etc/nginx/nginx.conf - - -:: - - To have launchd start nginx at login: - ln -sfv /usr/local/opt/nginx/*.plist ~/Library/LaunchAgents - Then to load nginx now: - launchctl load ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist - Or, if you don't want/need launchctl, you can just run: - nginx - - - -Nginx setup on Lubuntu 14.04.1 LTS ----------------------------------- - -Docroot is: /usr/share/nginx/html + $ ./docker_run_servers.sh +4: access http://localhot:8001 to acess nginx with no delay +5: access http://localhot:8002 to acess nginx with delay +6: access http://localhot:8003 to acess nginx with delay and errors +You can use this address to check Chapter 18 example of downloading flags diff --git a/17-futures/countries/docker_run_servers.sh b/17-futures/countries/docker_run_servers.sh new file mode 100755 index 0000000..f397b60 --- /dev/null +++ b/17-futures/countries/docker_run_servers.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env bash + +docker run -p 8002:8002 -p 8001:8001 -p 8003:8003 -it renzon/flupy-flags:run-servers ./run_servers.sh \ No newline at end of file diff --git a/17-futures/countries/flags.py b/17-futures/countries/flags.py index 7731bc4..3c2fde0 100644 --- a/17-futures/countries/flags.py +++ b/17-futures/countries/flags.py @@ -13,11 +13,14 @@ import os import time import sys +from itertools import product import requests # <1> -POP20_CC = ('CN IN US ID BR PK NG BD RU JP ' - 'MX PH VN ET EG DE IR TR CD FR').split() # <2> +import string + + +POP20_CC = tuple(''.join(t) for t in product(string.ascii_uppercase,string.ascii_uppercase)) # <2> BASE_URL = 'http://flupy.org/data/flags' # <3> diff --git a/17-futures/countries/nginx.conf b/17-futures/countries/nginx.conf new file mode 100644 index 0000000..9e25e5a --- /dev/null +++ b/17-futures/countries/nginx.conf @@ -0,0 +1,68 @@ +# For more information on configuration, see: +# * Official English Documentation: http://nginx.org/en/docs/ +# * Official Russian Documentation: http://nginx.org/ru/docs/ + +user nginx; +worker_processes auto; +error_log /var/log/nginx/error.log; +pid /run/nginx.pid; + +# Load dynamic modules. See /usr/share/nginx/README.dynamic. +include /usr/share/nginx/modules/*.conf; + +events { + worker_connections 1024; +} + +http { + log_format main '$remote_addr - $remote_user [$time_local] "$request" ' + '$status $body_bytes_sent "$http_referer" ' + '"$http_user_agent" "$http_x_forwarded_for"'; + + access_log /var/log/nginx/access.log main; + + sendfile on; + tcp_nopush on; + tcp_nodelay on; + keepalive_timeout 65; + types_hash_max_size 2048; + + include /etc/nginx/mime.types; + default_type application/octet-stream; + + # Load modular configuration files from the /etc/nginx/conf.d directory. + # See http://nginx.org/en/docs/ngx_core_module.html#include + # for more information. + include /etc/nginx/conf.d/*.conf; + + server { + listen 80 default_server; + listen [::]:80 default_server; + server_name _; + root /usr/share/nginx/html; + + # Load configuration files for the default server block. + include /etc/nginx/default.d/*.conf; + + location / { + } + + error_page 404 /404.html; + location = /40x.html { + } + + error_page 500 502 503 504 /50x.html; + location = /50x.html { + } + } + + server { + listen 8001; + + location /flags/ { + root /app/; + } + } + + +} diff --git a/17-futures/countries/run_servers.sh b/17-futures/countries/run_servers.sh new file mode 100755 index 0000000..40db0ae --- /dev/null +++ b/17-futures/countries/run_servers.sh @@ -0,0 +1,14 @@ +#!/bin/bash + +#Start nginx on port 8001 +nginx + +#Start vaurien with delay in background on port 8002 +vaurien --protocol http --backend localhost:8001 \ + --proxy 0.0.0.0:8002 \ + --behavior 100:delay --behavior-delay-sleep 1 & + +#Start vaurien with delay and error on port 8003 +vaurien --protocol http --backend localhost:8001 \ + --proxy 0.0.0.0:8003 \ + --behavior 25:error,50:delay --behavior-delay-sleep .5 \ No newline at end of file diff --git a/17-futures/countries/vaurien_delay.sh b/17-futures/countries/vaurien_delay.sh deleted file mode 100755 index f5a7ffb..0000000 --- a/17-futures/countries/vaurien_delay.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/bash -vaurien --protocol http --backend localhost:8001 \ - --proxy localhost:8002 \ - --behavior 100:delay --behavior-delay-sleep .5 diff --git a/17-futures/countries/vaurien_error_delay.sh b/17-futures/countries/vaurien_error_delay.sh deleted file mode 100755 index f13d02e..0000000 --- a/17-futures/countries/vaurien_error_delay.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/bash -vaurien --protocol http --backend localhost:8001 \ - --proxy localhost:8003 \ - --behavior 25:error,50:delay --behavior-delay-sleep .5 \ No newline at end of file diff --git a/18-asyncio/contador.py b/18-asyncio/contador.py new file mode 100644 index 0000000..e5d844a --- /dev/null +++ b/18-asyncio/contador.py @@ -0,0 +1,27 @@ +import time +from threading import Thread + + +def contar(nome, n): + for i in range(n): + # time.sleep(.001) + print(nome, i) + + +def contar_depois(t1, t2): + contar(2, 100) + print('Aguardando t1') + t1.join() + print('Aguardando t2') + t2.join() + print('Depois do join') + + contar(2, 100) + + +threads = tuple(Thread(target=contar, args=(i, 10000)) for i in range(2)) +depois_t = Thread(target=contar_depois, args=threads) + +for t in threads: + t.start() +depois_t.start()