简体   繁体   中英

Using third-party Python module that blocks Flask application

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 to parse Excel files a user has uploaded. 来解析用户上传的Excel文件。

I've monkey patched the builtins like this in the first lines of the application:

import os
import eventlet
if os.name == 'nt':
    eventlet.monkey_patch(os=False)
else:
    eventlet.monkey_patch()

Then I use the following to start the task that contains calls to . 调用的任务。

socketio.start_background_task(my_background_task)

What is the appropriate way to now call these other modules so that my application runs smoothly? Is the module to start another process within the greened thread the right way? 模块是否以正确的方式在绿化线程内启动另一个进程?

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

eventlet.tpool.execute(xlrd_read, file_path, other=arg)

Execute meth in a Python thread, blocking the current coroutine/ greenthread until the method completes.

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.

[1] http://eventlet.net/doc/threading.html

[2] https://github.com/eventlet/eventlet/issues

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM