<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by David McKeown on Medium]]></title>
        <description><![CDATA[Stories by David McKeown on Medium]]></description>
        <link>https://medium.com/@DaveJMcKeown?source=rss-cc05c2666457------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*bbgkiirztEdeqaw1fr9tZA.jpeg</url>
            <title>Stories by David McKeown on Medium</title>
            <link>https://medium.com/@DaveJMcKeown?source=rss-cc05c2666457------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 22:44:46 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@DaveJMcKeown/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Deploying Scipy & Numpy With Docker]]></title>
            <link>https://medium.com/google-cloud/deploying-scipy-numpy-with-docker-205e9afac3b0?source=rss-cc05c2666457------2</link>
            <guid isPermaLink="false">https://medium.com/p/205e9afac3b0</guid>
            <category><![CDATA[google-cloud-platform]]></category>
            <category><![CDATA[numpy]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[scipy]]></category>
            <dc:creator><![CDATA[David McKeown]]></dc:creator>
            <pubDate>Tue, 04 Jul 2017 03:27:52 GMT</pubDate>
            <atom:updated>2017-07-04T03:33:09.777Z</atom:updated>
            <content:encoded><![CDATA[<h4><em>Originally posted on 02 Sept 2015 on previous blog</em></h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dycaOuAjZWl1gpj4G_1kWg.jpeg" /></figure><p><a href="https://www.docker.com/">Docker</a>’s pretty decent right? Google agrees. It is a core part of their cloud hosting platform.</p><h3>Bit of Background</h3><p><strong>TL;DR</strong>: No pip wheel of SciPy and NumPy for Debian <strong>&gt;</strong> we have to compile it from source during pip install <strong>&gt;</strong> takes a long time.</p><p>The last few weeks have seen me working with all the major hosting providers in an effort to find the most suitable home for a particular Python project. A consistent thorn that reared it’s head on each platform was the need to compile <a href="http://www.scipy.org/">SciPy</a>. The thorn is not so much getting SciPy to compile — that’s easy enough usually — it is the time it takes if you are compiling from source.</p><p>If you only work with SciPy on your MacBook you’re probably wondering what I’m talking about. pip install scipy takes less than 30 seconds for you? Aye, it does... on MacOS. This is thanks to a neat little feature the pip crew introduced a while back called <a href="https://pip.pypa.io/en/latest/reference/pip_wheel.html">pip wheel</a>. This lets maintainers of projects published to the package index pre-build their projects and let you download those. Short story, you download a copy of SciPy already compiled for your hardware and OS.</p><p>The clever folks who maintain packages, like SciPy, can build varios wheels for popular operating systems and hardware flavors. Unfortunately, the SciPy team only publishes a wheel for MacOS at this time.</p><p>In the end, this means that when we pip install scipy numpy on our Linux VM we have to wait for them to compile; and that takes way too long. Run your CI job to publish a new version and it takes 20 minutes? Not ideal; also potential for failure is higher.</p><h3>The Solution</h3><p>I decided the best approach would be to create 2 Docker images.</p><p><a href="https://dajos.azurewebsites.net/deploying-scipy-to-docker/#wheel.docker"><strong>wheel.docker</strong></a> — The first Dockerfile is responsible for creating a /wheelhouse folder filled with built packages of everything from my applications requirements.txt file. This image would only need to built when the requirements.txt is changed. This is pretty rare when a project reaches a certain point of stability (production?).</p><p><a href="https://dajos.azurewebsites.net/deploying-scipy-to-docker/#main.docker"><strong>main.docker</strong></a> — The second Dockerfile will built from the first and pip install --no-index -f /wheelhouse. Meaning, it will use only the .whl packages we created in the first build. This will drastically reduce the time it takes to install our requirements into the Virtual Environment on each deployment.</p><p>The contents of the each Dockerfile are below.</p><p>Taking this a step further. I mentioned the CI jobs earlier. I created 2 jobs for this deployment. The main job is responsible for running the main.docker build and testing before deploying. It also has the wheel job as a dependency. The wheel job will check if requirments.txt has been modified since the last build. If it has, then we have a long build as it compiles the wheels, but almost always it&#39;ll skip this build.</p><p>Net result is most deployments now take less than a minute (including tests and publish) vs the ~30 minutes of a full build from source.</p><h3>wheel.docker</h3><p>When building remember to tag it appropriately for main.dockerto FROM .... For example: docker build -t myapp/wheels/build -f wheel.docker .</p><pre># [START docker]<br>FROM google/python  <br># [END docker]</pre><pre># [START system dependencies]<br>RUN apt-get install -y \  <br>    libpng-dev \<br>    freetype* \<br>    libblas-dev \<br>    liblapack-dev \<br>    libatlas-base-dev \<br>    gfortran<br># [END system dependencies]</pre><pre># [START app environment]<br>WORKDIR /app  <br>ADD requirements.txt /app/requirements.txt  <br>RUN virtualenv /env  <br>RUN /env/bin/pip install --upgrade pip  <br># [END app environment]</pre><pre># [START install &amp; configure wheel]<br>RUN /env/bin/pip install wheel  <br>ENV PIP_WHEEL_DIR=/wheelhouse  <br>ENV WHEELHOUSE=/wheelhouse  <br>ENV PIP_FIND_LINKS=/wheelhouse  <br># [START install &amp; configure wheel]</pre><pre>RUN while read p; do /env/bin/pip wheel $p; done &lt; requirements.txt</pre><h3>main.docker</h3><p>Might need to change the ENTRYPOINT if you use this file.</p><pre># [START docker]<br>FROM myapp/wheels/build  <br># [END docker]</pre><pre># [START configure app env]<br>WORKDIR /app</pre><pre>RUN while read p; \  <br>    do /env/bin/pip install --no-index -f /wheelhouse $p; \<br>    done &lt; requirements.txt</pre><pre>ADD . /app  <br># [END configure app env]</pre><pre>EXPOSE 8080  <br>CMD []</pre><pre># [START Application Definition]<br>ENTRYPOINT [&quot;/env/bin/python&quot;, &quot;/app/wsgi.py&quot;]  <br># [END Application Definition]</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=205e9afac3b0" width="1" height="1" alt=""><hr><p><a href="https://medium.com/google-cloud/deploying-scipy-numpy-with-docker-205e9afac3b0">Deploying Scipy &amp; Numpy With Docker</a> was originally published in <a href="https://medium.com/google-cloud">Google Cloud - Community</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Deploying SciPy into AWS Elastic Beanstalk]]></title>
            <link>https://medium.com/@DaveJMcKeown/deploying-scipy-into-aws-elastic-beanstalk-2e5e481155de?source=rss-cc05c2666457------2</link>
            <guid isPermaLink="false">https://medium.com/p/2e5e481155de</guid>
            <category><![CDATA[elastic-beanstalk]]></category>
            <category><![CDATA[scipy]]></category>
            <category><![CDATA[numpy]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[docker]]></category>
            <dc:creator><![CDATA[David McKeown]]></dc:creator>
            <pubDate>Wed, 22 Mar 2017 17:36:36 GMT</pubDate>
            <atom:updated>2017-07-04T03:34:07.131Z</atom:updated>
            <content:encoded><![CDATA[<h4>Originally posted on 08 Jan 2015 on previous blog</h4><p>Some things sound so simple — such as the title of this post — and naturally are, but this is not one of those things. Chances are with you being here on this page you are already aware that all is not so cut and dry!</p><p>It must be said, the challenge I had here was further compounded by my limited Python background. I had maybe 2 months, at most, of “light” experience with Python.</p><p>I may go into a little more detail than you really care for, if you are just here for the solution, scroll down to the solution section.</p><h3>Background</h3><p>The application being deployed was a small service with a single route which would run our model. The model leveraged SciPy and NumPy. The service itself was a Flask application with a couple of routes — a GET route to verify the service running and a POST route call to execute the model.</p><p>That is the basic high level overview of what needed to be deployed, there was a little more to it than that — schema validation and such — but not necessary to go into detail here.</p><p>I ran into a few issues up front, simple things, those silly things you immediately think “should have thought of that”. All easy enough to overcome. However, the challenges soon became a little more obscure in nature.</p><p>That’s when I decided that a bit of Googling would be wise. I landed on a few different blog posts and Github repos related to EB, SciPy and NumPy. All proposing different solutions. I was a little put off by some of the solutions being proposed.</p><p>The solutions suggested things like:</p><ul><li>Removing SciPy and NumPy from the requirements file and manually installing using container level commands in the EB configuration. Don’t like that idea much.</li><li>Use Yum to install the packages and then copy them with container commands. Again, I wasn’t keen on this much.</li><li>Use a shell script to install the dependencies, again involved dropping from the requirements file.</li></ul><p>I gave each a try, some I knew I was never going to use as the final solution — dropping dependencies from our dependency file was just not viable for me. Others didn’t work at all. All were tempermental at best in deployments.</p><p>With that said though they were all valuable. They helped me make sure I wasn’t overlooking a kernal level dependency and that I wasn’t just being dumb. <em>Both good things to know.</em> Now with that I decided I’d give it a stab at going back to formula.</p><p>My configuration would install kernal dependencies (gcc, python-devel, etc), and pip would install NumPy and SciPy into the virtual environment. This is where it got very interesting. The deployment went through without any issues, everything seemed good, logs were clean. Jumped into the browser to quickly test that simple GET route expecting to see the service respond to me. Unfortunately all it did was spin until the inevitable timeout.</p><p>Over to the logs I went. Nothing, no exceptions, no errors, the only thing was that the HTTP (Apache) log was showing the timeout. No headers were returned in the time limit. <em>Okay, great, but why?</em></p><p>I was able to narrow down the issue to SciPy imports. Specifically, importing from SciPy where compilation was involved. Deployments to EB with these lines removed would result in a responsive service; but of course the model (POST route) would be broken if called since dependencies were missing.</p><p>Now I was pretty confident that the issue was somewhere between Apache and/or WSGI. I decided a good way to test this theory would be to SSH to the AWS server and run the application directly with python. By doing this I was removing Apache and WSGI from the equation but keeping everything else the same. Sure enough, by running the application this way, the application worked just fine and SciPy imported and compiled without issue.</p><p>I decided a good next step that wouldn’t take much time was to try to mirror the stack to recreate the issue and for sanity — linux, apache, WSGI. I fired up a fresh VM, used yum to install the same packages in my EB config, setup the environment and setup the WSGI application.</p><p>Sure enough, same issue! I now had the issue completely isolated to running SciPy with WSGI. I was even able to recreate it without the VM just using Apache on my laptop.</p><h3>Solution</h3><p>The solution was rather simple once the root of the problem had been isolated to WSGI and is <a href="https://code.google.com/p/modwsgi/wiki/ApplicationIssues#Python_Simplified_GIL_State_API">well documented</a>.</p><blockquote>The consequences of attempting to use a C extension module for Python which is implemented against the simplified API for GIL state management in any sub interpreter besides the first, is that the code is likely to deadlock or crash the process. The only way around this issue is to ensure that any WSGI application which makes use of C extension modules which use this API, only runs in the very first Python sub interpreter created when Python is initialized.</blockquote><p>The solution is to force the WSGI application to use the very first Python sub interpreter. This can be done by adding the following line to your Apache configuration: (I verified this on the AWS instance, the VM and on my mac locally)</p><pre>WSGIApplicationGroup %{GLOBAL}</pre><p>The only thing I now had to do was make this part of my EB configuration so that any time this application deploys, or a scaling event happens, the line is added to Apache’s configuration.</p><p>Here is my new EB application configuration file:</p><pre>packages:  <br>  yum:<br>    make: []<br>    gcc-c++: []<br>    gcc-gfortran: []<br>    python-devel: []<br>    atlas-sse3-devel: []<br>    lapack-devel: []<br>    libpng-devel: []<br>    freetype-devel: []<br>    zlib-devel: []<br>container_commands:  <br>  AddGlobalWSGIGroupAccess: <br>    command: &quot;if ! grep -q &#39;WSGIApplicationGroup %{GLOBAL}&#39; ../wsgi.conf ; then echo &#39;WSGIApplicationGroup %{GLOBAL}&#39; &gt;&gt; ../wsgi.conf; fi;&quot;</pre><p>This, for me, is a little more elegant as a solution than removing dependencies from the dependency file. Your requirements.txt should and will contain everything necessary to launch your application. It has also, in my tests, proven completely stable in deployments and scaling.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2e5e481155de" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>