Using multiple cores with Python and Eventlet
<p>I have a Python web application in which the client (<a href="http://emberjs.com/" rel="nofollow">Ember.js</a>) communicates with the server via WebSocket (I am using <a href="https://flask-socketio.readthedocs.io/en/latest/" rel="nofollow">Flask-SocketIO</a>). 
Apart from the WebSocket server the backend does two more things that are worth to be mentioned:</p>

<ul>
<li>Doing some image conversion (using <a href="http://www.graphicsmagick.org/" rel="nofollow">graphicsmagick</a>)</li>
<li>OCR incoming images from the client (using <a href="https://github.com/tesseract-ocr" rel="nofollow">tesseract</a>)</li>
</ul>

<p>When the client submits an image its entity is created in the database and the id is put in an image conversion queue. The worker grabs it and does image conversion. After that the worker puts it in the OCR queue where it will be handled by the OCR queue worker.</p>

<p>So far so good. The WS requests are handled synchronously in separate threads (Flask-SocketIO uses Eventlet for that) and the heavy computational action happens asynchronously (in separate threads as well).</p>

<p>Now the problem: the whole application runs on a <strong>Raspberry Pi 3</strong>. If I do not make use of the 4 cores it has I only have <strong>one ARMv8 core clocked at 1.2 GHz</strong>. This is very little power for OCR. So I decided to find out how to use multiple cores with Python. Although I read about the problems with the <a href="http://www.infoworld.com/article/3079037/open-source-tools/multicore-python-a-tough-worthy-and-reachable-goal.html" rel="nofollow">GIL</a>) I found out about <a href="https://docs.python.org/2/library/multiprocessing.html" rel="nofollow">multiprocessing</a> where it says <code>The multiprocessing package offers both local and remote concurrency, effectively side-stepping the Global Interpreter Lock by using subprocesses instead of threads.</code>. Exactly what I wanted. So I instantly replaced the </p>

<pre><code>from threading import Thread
thread = Thread(target=heavy_computational_worker_thread)
thread.start()
</code></pre>

<p>by</p>

<pre><code>from multiprocessing import Process
process = Process(target=heavy_computational_worker_thread)
process.start()
</code></pre>

<p>The queue needed to be handled by the multiple cores as well So i had to change</p>

<pre><code>from queue import Queue
queue = multiprocessing.Queue()
</code></pre>

<p>to</p>

<pre><code>import multiprocessing
queue = multiprocessing.Queue()
</code></pre>

<p>as well. Problematic: the queue and the Thread libraries are <a href="http://eventlet.net/doc/patching.html" rel="nofollow">monkey patched</a> by Eventlet. If I stop using the monkey patched version of Thread and Queue and use the one from <code>multiprocsssing</code> instead then the request thread started by Eventlet blocks forever when accessing the queue.</p>

<p>Now my question:</p>

<p><strong>Is there any way I can make this application do the OCR and image conversion on a separate core?</strong></p>

<p>I would like to keep using WebSocket and Eventlet if that's possible. The advantage I have is that the only communication interface between the processes would be the queue. </p>

<p>Ideas that I already had:
 - Not using a Python implementation of a queue but rather using I/O. For example a dedicated Redis which the different subprocesses would access
 - Going a step further: starting every queue worker as a separate Python process (e.g. python3 wsserver | python3 ocrqueue | python3 imgconvqueue). Then I would have to make sure myself that the access on the queue and on the database would be non-blocking</p>

<p>The best thing would be to keep the single process and make it work with multiprocessing, though.</p>

<p>Thank you very much in advance</p>

<p>Eventlet is currently incompatible with the multiprocessing package. There is an open issue for this work: <a href="https://github.com/eventlet/eventlet/issues/210" rel="nofollow">https://github.com/eventlet/eventlet/issues/210</a>.</p>

<p>The alternative that I think will work well in your case is to use Celery to manage your queue. Celery will start a pool of worker processes that wait for tasks provided by the main process via a message queue (RabbitMQ and Redis are both supported).</p>

<p>The Celery workers do not need to use eventlet, only the main server does, so this frees them to do whatever they need to do without the limitations imposed by eventlet.</p>

<p>If you are interested in exploring this approach, I have a complete example that uses it: <a href="https://github.com/miguelgrinberg/flack" rel="nofollow">https://github.com/miguelgrinberg/flack</a>.</p>

python multithreading multiprocessing eventlet flask-socketio