多线程 python 调用 Google Cloud Vision API

Multi-threading python calls to Google Cloud Vision API

我是 运行 macOS 机器支持的 Docker 容器中的服务器,我需要从中发送几张图像供 Google Cloud Vision [=59] 处理=].

我必须能够最大限度地减少上传和处理图像所花费的时间。

我首先将对 GCV 的调用包装在 Queue.QueueThreading.Thread 中,但这偶尔会使我的代码崩溃(不会被 python Exception 困住)因此:

E1121 14:15:10.902211037   25448 sync_posix.c:38]            assertion failed: pthread_mutex_destroy(mu) == 0

根据几个 github 线程,这是受 gRPC 启发(或 httplib?)bug/feature,但我找不到简单解决方法的步骤 - 参见例如https://github.com/grpc/grpc/issues/11184 and https://github.com/grpc/grpc/issues/10909

鉴于这似乎是一个普遍存在的问题,没有明确的解决方案,最好的缓解方法是什么?

是否像使用单个批处理调用(但整体速度如何?)到 GCV 一样简单?没有其他方法可以安全地对调用进行线程化吗?

更新:

由于担心损坏,我开始将 gRPC 回滚到 v1。2.x a la

https://github.com/grpc/grpc/issues/10909#issuecomment-302581954

除了我必须在 Docker 容器中将 /usr/local/lib 添加到 LD_LIBRARY_PATH 以获取 libprotobuf.so.12

我没有对任何其他 python 软件包进行任何更改。

然后我做了:

pip install grpcio==1.2.1

显然这会变得很昂贵,但与之前的 1/3 调用相比,崩溃率是 <1/20 调用(并且还在增加)。

现在:我怎样才能最终测试这是否已修复?

这是我使用的 Dockerfile 的相关部分(基于 https://github.com/grpc/grpc/issues/10909#issuecomment-302581954

# gRPC fix (rollback to v1.2.x to mitigate mutex crashing bug)
RUN apt-get install -y build-essential autoconf libtool
RUN apt-get install -y libgflags-dev libgtest-dev
RUN apt-get install -y clang libc++-dev
RUN apt-get install -y sudo unzip locate
RUN mkdir -p /app/src

WORKDIR /app/src
RUN git clone --branch v1.2.x https://github.com/grpc/grpc

WORKDIR /app/src/grpc
RUN git submodule foreach git clean -xfd
RUN git submodule update --init

WORKDIR /app/src/grpc/third_party/protobuf
RUN ./autogen.sh
RUN ./configure
RUN make -j2
RUN sudo make install

WORKDIR /app/src/grpc
RUN sudo make install
ENV LD_LIBRARY_PATH=${LD_LIBRARY}:/usr/local/lib
RUN pip install --trusted-host pypi.python.org grpcio==1.2.1
RUN pip install --trusted-host pypi.python.org google-cloud-vision

我会把这个打开几天,看看是否有人回应。