How to properly use SQLAlchemy with Dask distributed jobs

I’m having a lot of trouble using SQLAlchemy to store the results of distributed jobs. Previously, I had an abstraction layer between the SQLAlchemy objects and the main code, meaning that all database objects were converted into pure Python objects before being used in the main logic (and vice-versa). I am now trying to use the database objects directly to reduce overhead. However, I get unexpected clashes between database objects.

Here is a minimal example which puzzles me:

import pytest
from typing import List, Optional

from sqlalchemy import (
    String,
    ForeignKey,
)
from sqlalchemy.orm import (
    Mapped,
    mapped_column,
    relationship,
    declarative_base,
    Session,
)
from sqlalchemy.engine import create_engine

from dask.distributed import (
    Client,
    LocalCluster,
)


Base = declarative_base()


def get_fields(obj, *fields):
    ret = {}
    for f in fields:
        if hasattr(obj, f):
            ret[f] = getattr(obj, f)
    return ret


def set_fields(obj, **fields):
    for k, v in fields.items():
        setattr(obj, k, v)


class Parent(Base):
    __tablename__ = "parent"

    name: Mapped[str] = mapped_column(String(100), primary_key=True)

    child1_name: Mapped[Optional[str]] = mapped_column(ForeignKey("child.name"))
    child1: Mapped["Child"] = relationship(
        back_populates="parents", foreign_keys=child1_name, lazy="select"
    )

    child2_name: Mapped[Optional[str]] = mapped_column(
        ForeignKey("child.name", ondelete="SET NULL")
    )
    child2: Mapped[Optional["Child"]] = relationship(
        back_populates="child2_of",
        foreign_keys=child2_name,
        lazy="select",
        passive_deletes=True,
    )


class Child(Base):
    __tablename__ = "child"

    name: Mapped[str] = mapped_column(String(100), primary_key=True)

    parents: Mapped[Optional[List["Parent"]]] = relationship(
        back_populates="child1",
        primaryjoin="Child.name==foreign(Parent.child1_name)",
    )

    # Removing this backlink (and the reference in Parent.child2) makes the test pass
    child2_of: Mapped[Optional[List["Parent"]]] = relationship(
        back_populates="child2",
        primaryjoin="Child.name==foreign(Parent.child2_name)",
    )


class Job:
    def __init__(self, target):
        self.target = target
        self.results = {}

    def set(self, **kwargs):
        self.results.update(**kwargs)

    def run(self):

        self.target.child2 = Child(name="prod")

        self.set(
            **get_fields(
                self.target,
                "child2",
            )
        )

        """
        # Works fine
        self.results["child2"] = Child(name="child2")
        """

        return self

    def process_results(self, sess):
        self.target = sess.get(Parent, self.target.name)

        set_fields(self.target, **self.results)

        sess.commit()


@pytest.fixture()
def client():
    cluster = LocalCluster(n_workers=1, processes=False)
    client = Client(cluster)

    yield client

    client.close()
    cluster.close()


def test_job(client):
    eng = create_engine(
        "sqlite://",
    )

    Base.metadata.create_all(eng)
    sess = Session(eng)

    parent = Parent(name="parent")
    sess.add(parent)
    sess.commit()

    parent = sess.get(Parent, parent.name)

    job = Job(parent)
    job = client.gather(
        client.submit(
            job.run,
        )
    )

    """
    # Works fine
    job = job.run()
    """

    assert "child2" in job.results

    assert job.results["child2"] is not None

    job.process_results(sess)

The test (pytest sample.py) fails with this error:

sqlalchemy.exc.InvalidRequestError: Can't attach instance <Parent at 0x14cedd3985e0>; another instance with key (<class 'min.Parent'>, ('parent',), None) is already present in this session.

This only occurs when using Dask (i.e., another thread). It also only occurs if Job.target is modified during the job’s execution, which I find odd since it stays the same Parent object. Moreover, removing the backreference Child.child2_of also makes this error go away, which doesn’t make any sense to me.

  1. What explains this behavior?
  2. More generally, what would be a better way to run Job objects and save the results with SQLAlchemy in the same classes?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật