在 Python 和 Google 云服务中使用多处理时出现 SSL 错误
SSL error using multiprocessing in Python with Google Cloud services
在我在 Flask 上的应用程序中,我在一批文件中使用多处理 - 用户上传一个包含许多 pdf 文件的 .zip - 上传后,在数据库上为每个文件创建一个新实体,然后是一个线程启动并调用多处理池,以便每个文件启动一个进程,该进程与 Google 云服务(例如 Google 存储和 Google 数据存储)进行交互。
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
在文件对象内部,有与 Google 数据存储和 Google 存储的简单交互 - 例如从存储桶中重新读取文件或修改数据。一切都在本地顺利进行......但是在使用 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
有人知道导致此错误的原因吗?我做了一些研究,发现了一些与 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
在我在 Flask 上的应用程序中,我在一批文件中使用多处理 - 用户上传一个包含许多 pdf 文件的 .zip - 上传后,在数据库上为每个文件创建一个新实体,然后是一个线程启动并调用多处理池,以便每个文件启动一个进程,该进程与 Google 云服务(例如 Google 存储和 Google 数据存储)进行交互。
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
在文件对象内部,有与 Google 数据存储和 Google 存储的简单交互 - 例如从存储桶中重新读取文件或修改数据。一切都在本地顺利进行......但是在使用 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
有人知道导致此错误的原因吗?我做了一些研究,发现了一些与 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