Using third-party Python module that blocks Flask application
<p>My application that uses websockets also makes use of several third-party Python modules that appear to be written in way that blocks the rest of the application when called. For example, I use <b>xlrd</b> to parse Excel files a user has uploaded.</p>

<p>I've monkey patched the builtins like this in the first lines of the application:</p>

<pre><code>import os
import eventlet
if os.name == 'nt':
    eventlet.monkey_patch(os=False)
else:
    eventlet.monkey_patch()
</code></pre>

<p>Then I use the following to start the task that contains calls to <b>xlrd</b>.</p>

<pre><code>socketio.start_background_task(my_background_task)
</code></pre>

<p>What is the appropriate way to now call these other modules so that my application runs smoothly? Is the <b>multiprocessing</b> module to start another process within the greened thread the right way?</p>

<ul>
<li>First you should try thread pool [1].</li>
<li>If that doesn't work as well as you want, please submit an issue [2] and go with multiprocessing as workaround.</li>
</ul>

<p><code>eventlet.tpool.execute(xlrd_read, file_path, other=arg)</code></p>

<blockquote>
  <p>Execute meth in a Python thread, blocking the current coroutine/ greenthread until the method completes.</p>
  
  <p>The primary use case for this is to wrap an object or module that is not amenable to monkeypatching or any of the other tricks that Eventlet uses to achieve cooperative yielding. With tpool, you can force such objects to cooperate with green threads by sticking them in native threads, at the cost of some overhead.</p>
</blockquote>

<p>[1] <a href="http://eventlet.net/doc/threading.html" rel="nofollow">http://eventlet.net/doc/threading.html</a></p>

<p>[2] <a href="https://github.com/eventlet/eventlet/issues" rel="nofollow">https://github.com/eventlet/eventlet/issues</a></p>

python flask eventlet flask-socketio