How do I run two processes in Procfile?
<p>I have a Flask app where I have embedded a Bokeh server graph and I am not being able to have them both working on Heroku. I am trying to deploy on Heorku and I can either start the Bokeh app or the Flask app from the Procfile, but not both at the same time. Consequently, either the content served with Flask will show, or the Bokeh graph. </p>

<p>When I deploy with the following line in Procfile, the Bokeh content shows up on the webpage, but not nothing from Flask:</p>

<pre><code>web: bokeh serve --port=$PORT --host=bokehapp.herokuapp.com --host=* --address=0.0.0.0 --use-xheaders bokeh_script.py
</code></pre>

<p>If I deploy with the following, I only get the Flask content, not the Bokeh graph:</p>

<pre><code>web: gunicorn app:app
</code></pre>

<p>In the second case, I am starting Bokeh inside the app.py Flask script using a subprocess:</p>

<pre><code>bokeh_process = subprocess.Popen(
    ['bokeh', 'serve','--allow-websocket-origin=bokehapp.herokuapp.com','--log-level=debug','standard_way_with_curdoc.py'], stdout=subprocess.PIPE)
</code></pre>

<p>The Heroku logs don't show any errors.</p>

<p>I also tried a third alternative:</p>

<pre><code>web: bokeh serve --port=$PORT --host=bokehapp.herokuapp.com --host=* --address=0.0.0.0 --use-xheaders bokeh_script.py
web: gunicorn app:app
</code></pre>

<p>And that shows Flask content only. It seems only second worker is being considered.</p>

<p>So, my question is how modify the Procfile to consider both processes? 
Or maybe I am approaching this wrong all together? Any clue you can give would be appreciated. </p>

<p>Each Heroku dyno gets allocated a single public network port, which you can get from the <code>$PORT</code> variable, pre-assigned by Heroku before launching your app. The unfortunate side effect of this is that you can have only one public web server running in a dyno.</p>

<p>I think the first thing that you need to do is route all requests to your application through your Flask server. For example, you can add a <code>/bokeh/&lt;path:path&gt;</code> route to your Flask app that forwards all requests to the bokeh server using <a href="http://docs.python-requests.org/en/master/" rel="nofollow">requests</a>, then sends the response back to the client. With this change, you now have a single public web server, and the bokeh server can run as a background service without public access.</p>

<p>So now you can deploy your Flask app to Heroku, and have it receive both its own requests and the requests for the bokeh server. The next step is to figure out where to deploy the bokeh server.</p>

<p>The proper way to do this is to deploy bokeh on a separate dyno. The Flask dyno will know how to forward requests to bokeh because you will have a configuration item with the bokeh server URL.</p>

<p>If you want to have everything hosted on a single dyno I think you can as well, though I have never tried this myself and can't confirm it is viable. The all in one configuration is less ideal and not what Heroku recommends, but according to the <a href="https://blog.heroku.com/new_dyno_networking_model#networking-improvements" rel="nofollow">dyno networking documentation</a> it appears you can privately listen on any network ports besides <code>$PORT</code>. Those are not exposed publicly, but the doc seems to suggest the processes running inside a dyno can communicate through private ports. So for example, you can start the Flask app on <code>$PORT</code>, and the bokeh server on <code>$PORT + 1</code>, and have Flask internally forward all the bokeh requests to <code>$PORT + 1</code>.</p>

python heroku flask bokeh procfile