Ken*_*res 7 python ssl multiprocessing google-cloud-storage google-cloud-datastore
在 Flask 上的应用程序中,我在一批文件中使用多重处理 - 用户上传包含许多 pdf 文件的 .zip - 上传后,会在数据库上为每个文件创建一个新实体,然后启动一个线程并调用多重处理池,因此每个文件都会启动一个与 Google Cloud 服务(例如 Google Storage 和 Google Datastore)进行交互的进程。
import threading
import multiprocessing
import sys
class ProcessMulti(threading.Thread):
def __init__(self, files_ids):
self.files_ids = files_ids
super().__init__()
def run(self):
with multiprocessing.Pool(processes=multiprocessing.cpu_count()) as pool:
for i, _ in enumerate(pool.imap_unordered(process_one, self.files_ids), 1):
sys.stderr.write('\rdone {0:%}'.format(i/len(self.files_ids)))
def process_one(file_id):
print("Process started by {}".format(file_id))
file = File(file_id)
file.process()
print("Process finished by {}".format(file_id))
return file.id
Run Code Online (Sandbox Code Playgroud)
在 File 对象内部,与 Google Datastore 和 Google Storage 进行一些简单的交互 - 例如从存储桶中删除文件或修改数据。一切都在本地顺利运行...但是在使用 SSL 连接的生产中,当尝试启动该过程时,会抛出以下错误并且根本没有任何反应:
Process started by 5377634535997440
E1004 15:49:32.711329522 32255 ssl_transport_security.cc:476] Corruption detected.
E1004 15:49:32.711356181 32255 ssl_transport_security.cc:452] error:100003fc:SSL routines:OPENSSL_internal:SSLV3_ALERT_BAD_RECORD_MAC
E1004 15:49:32.711361146 32255 secure_endpoint.cc:208] Decryption error: TSI_DATA_CORRUPTED
Run Code Online (Sandbox Code Playgroud)
任何人都知道导致此错误的原因是什么?我做了一些研究,发现了一些与 SSL 套接字过载相关的错误...但我不知道可以采取哪些操作来修复该问题,也不知道具有类似性能的多处理的替代方案。谢谢。
我最终将多处理和线程操作交换到 celery 任务队列,因为在连接到 gcloud 服务时存在一些我无法克服的线程安全问题。Celery 实现对于我的应用程序上的许多多个异步任务来说是一个很好的解决方案。
#Import celery instance with app context already set
from main_app import celery
@celery.task
def process_one(file_id):
print("Process started by {}".format(file_id))
file = File(file_id)
file.process()
print("Process finished by {}".format(file_id))
return file.id
Run Code Online (Sandbox Code Playgroud)