小编the*_*ORN的帖子

在一个线程中创建的 DatabaseWrapper 对象只能在同一个线程中使用。”尝试使用 celery 插入数据库时

我正在使用 celery 从 csv 读取数据并将其上传到 Postgres。celery 任务正在运行(我认为),但是 django 抛出了一个错误。我正在获取一个文件,将其转换为 Pandas,删除 2 列,然后转换为 numpy 并传递给 celery。

#tasks.py
@shared_task
def a(data):
    for x in data:
        date=x[0]
        date=datetime.datetime.strptime(date,"%m/%d/%Y")
        date=str(date.date())
        desc=x[1]
        amount=x[3]
        account=x[6]
        cat=x[5]
        account_type=x[4]
        #changing account type
        if account_type=="debit":
            account_type=False
        else:
            account_type=True
        #creating account if needed and assigning the the object to account
        try:
            account=t102_accounts.objects.get(c102_acc_name=account,c102_is_credit_fl=account_type)
        except:
            account=t102_accounts.objects.create(c102_acc_name=account,c102_is_credit_fl=account_type)
        #creating cat if needed and assigning the object
        try:
            cat=t100_code_lookup.objects.get(c100_code_name=cat)
        except:
            cat=t100_code_lookup.objects.create(c100_code_name=cat,c100_for_exp_fl=True,c100_for_income_fl=True)
        #creating the transaction if it is not existing already
        try:
            t106_transactions.objects.get(c106_trans_amount=amount,
            c106_trans_date=date,c102_trans_card_num=account,
            c100_trans_cat=cat,c106_trans_name=desc)
            continue …
Run Code Online (Sandbox Code Playgroud)

python django celery

5
推荐指数
1
解决办法
2842
查看次数

标签 统计

celery ×1

django ×1

python ×1