Domzou-kun / prpl

MIT License
3 stars 0 forks source link

Cannot access DB in class #2

Open tty198 opened 3 months ago

tty198 commented 3 months ago

Please describe the bug. _pickle.PicklingError: Can't pickle <class 'sqlalchemy.orm.session.Session'>: it's not the same object as sqlalchemy.orm.session. I get an exception that says

To reproduce To reproduce the following code:

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, Mapped, mapped_column, declarative_base
from sqlalchemy.dialects.postgresql import INTEGER, VARCHAR
from prpl import prpl
Base = declarative_base()
class tbl(Base):
    __tablename__ = 'testofprpl'
    id :Mapped[str] = mapped_column(VARCHAR, primary_key=True)
    data :Mapped[int] = mapped_column(INTEGER)

class test:
    def __init__(self) -> None:
        CONNSTR = f'postgresql+psycopg://postgres:postres@192.168.1.1:5432/test'
        engine = create_engine(CONNSTR, echo=False, pool_recycle=10)
        self.maker = sessionmaker(engine)
        self.session = self.maker()

    def withsession_param(self, session, id):
        calc_answer = id**2
        record = tbl(**{'id':str(id), 'data':calc_answer})
        session.add(record)

    def withoutsession_param(self, id):
        calc_answer = id**2
        record = tbl(**{'id':str(id), 'data':calc_answer})
        self.session.add(record)

    def start(self, session):
        target_list = list(range(0, 100))
        prpl(target_list, self.withsession_param, args={'session':session})
        prpl(target_list, self.withoutsession_param)

if __name__ == '__main__':
    t = test()
    session = t.maker()
    t.start(session)
Domzou-kun commented 3 months ago

Thanks for the bug report. I am currently on a business trip and do not have the testing environment with me, so it will take some time to fix and report the bug. I will provide the cause of the bug and the fixed code next week, so please wait a while.

From what I have seen of your code, there is nothing wrong with your code and it is most likely a potential bug in prpl, so I will look into it as soon as possible.