Python Django:multiprocessing中的查询发生Django.db.utils.OperationalError:SSL错误?

Python Django:multiprocessing中的查询发生Django.db.utils.OperationalError:SSL错误?,python,django,multiprocessing,django-queryset,Python,Django,Multiprocessing,Django Queryset,我使用了Django 1.11.1和PostgreSQL作为数据库 代码如下: 型号.py class Symbol(StockDataBaseModel): code = models.CharField(max_length=20) name = models.CharField(max_length=30) class DailyPrice(StockDataBaseModel): symbol = models.ForeignKey(Symbol)

我使用了
Django 1.11.1
PostgreSQL
作为数据库

代码如下:

型号.py

class Symbol(StockDataBaseModel):
    code = models.CharField(max_length=20)
    name = models.CharField(max_length=30)


class DailyPrice(StockDataBaseModel):
    symbol = models.ForeignKey(Symbol)

    date_time = models.DateTimeField()
    open = models.DecimalField(max_digits=15, decimal_places=2)
    high = models.DecimalField(max_digits=15, decimal_places=2)
    low = models.DecimalField(max_digits=15, decimal_places=2)
    close = models.DecimalField(max_digits=15, decimal_places=2)
获取数据部分

def _get_price_df(ticker, start_date, end_date):
    from data_manager.models import DailyPrice, Symbol

    symbol_set = Symbol.objects.prefetch_related('dailyprice_set').filter(name__iexact=ticker)
    if symbol_set.exists():
        symbol = symbol_set[0]
        if start_date and end_date:
            price_set = symbol.dailyprice_set.filter(Q(date_time__gte=start_date) & Q(date_time__lte=end_date))
        elif start_date:
            price_set = symbol.dailyprice_set.filter(Q(date_time__gte=start_date))
        elif end_date:
            price_set = symbol.dailyprice_set.filter(Q(date_time__lte=end_date))
        else:
            price_set = symbol.dailyprice_set.all()   
    else:
        raise ValueError("No such ticker exists : {}".format(ticker))


class StockDailyDataManager(object):

    def get_price_data_df(self, tickers, start_date=None, end_date=None):
        pool = Pool(12)
        df_list = pool.starmap(
            _get_price_df, [(ticker, start_date, end_date) for ticker in tickers]
        )
我尝试的是:

tickers = ['MSFT', 'AAPL', 'samsung'...]
pool = Pool(12)
df_list = pool.starmap(
    _get_price_df,
    [(ticker, start_date, end_date) for ticker in tickers]
)
发生错误:

django.db.utils.OperationalError: SSL error: decryption failed or bad record mac

我怎样才能解决这个问题

您必须首先关闭数据库连接,它将在每个进程中重新打开:

from django import db
db.connections.close_all()
请注意,这在迁移中对我不起作用。我想这与交易有关。因此,我不得不将工作转移到管理指挥部:

from multiprocessing import cpu_count, Pool, current_process
import tqdm

from django.core.management.base import BaseCommand
from django import db


def process(id):
  obj = <yourModel>.objects.get(pk=id)
  ... do something ....

class Command(BaseCommand):
    help = "do something in parallel"


    def handle(self, *args, **options):

        objects = <YourModel.objects.all().values_list('id', flat=True) 
        requests = list(objects)
        total = len(objects)

        # close db connections, they will be recreated automatically
        db.connections.close_all()

        pool = Pool(processes=cpu_count())
        for _ in tqdm.tqdm(pool.imap_unordered(process, objects), total=total):
            pass
从多处理导入cpu\u计数、池、当前\u进程
导入TQM
从django.core.management.base导入BaseCommand
从django导入数据库
def进程(id):
obj=.objects.get(pk=id)
... 做点什么。。。。
类命令(BaseCommand):
help=“并行地做某事”
def句柄(自身、*参数、**选项):

objects=我在一个管理命令中使用了它,在这个命令中我使用了一个多处理池。召唤

from django import db
db.connections.close_all()

在每一个过程的开始阶段都有帮助。然后,当从进程内部访问数据库时,进程创建了一个新连接,并且没有尝试共享来自父进程的原始连接。

可以尝试使用上下文管理器吗。例如,
将池(12)作为p:
然后
p.starmap(\u get\u price\u df,[(ticker,start\u date,end\u date)用于ticker中的ticker]
我尝试过,但也不起作用。。。