Hello, 

I have a heavy method of a class (not mapped with SQLA) that takes as 
argument some SQLA instances (with relationships), does some operation on 
them (without using any SQLA feature), and returns a subset of them. The 
logic was working before I added the SQLAlchemy persistence layer using 
pathos.multiprocessing.

In my full code I get a recursion depth exceeded error, and I was not able 
to reproduce the problem with a MWE. By the way with a MWE I get a "Can't 
picke <class 'sqlalchemy.ext.declarative.api.Base'>: it's not found as 
sqlalchemy.ext.declarative.api.Base". I suppose that I should define 
__getstate__  and __setstate__ magic methods stripping out all the SQLA 
"stuff" inside the instance. Is this right? Or there is something I'm not 
considering? Will I be able to reconstitute the instance after the 
pickling/unpickling? How does it work with the persistence with the 
underlying session? Are there any examples for this? 

Thanks in advance for the great support and help in this mailing list, 

from sqlalchemy import Column, Integer, String, DateTime, Float,\
    ForeignKey
from sqlalchemy.orm import relationship, sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from pyggdrasill.sql.schema import connect_db

from datetime import datetime

import pathos.multiprocessing as mpp

from functools import partial

Base = declarative_base()


class Sensor(Base):
    __tablename__ = 'sensor'

    id = Column(Integer, primary_key=True)
    name = Column(String(150))
    readings = relationship("Reading", backref='sensor',
                            cascade='all, delete-orphan')

    def __init__(self, name):
        self.name = name

    def read(self, room):
        return [
            Reading(
                self,
                room,
                datetime.now(),
                10.0),

            Reading(
                self,
                room,
                datetime.now(),
                20.0),

            Reading(
                self,
                room,
                datetime.now(),
                30.0)
        ]


class Room(Base):
    __tablename__ = 'room'

    id = Column(Integer, primary_key=True)
    name = Column(String(150))
    readings = relationship("Reading", backref='room',
                            cascade='all, delete-orphan')

    def __init__(self, name):
        self.name = name


class Reading(Base):
    __tablename__ = 'reading'

    id = Column(Integer, primary_key=True)
    date = Column(DateTime)
    voltage = Column(Float)

    sensor_id = Column(Integer, ForeignKey('sensor.id'), nullable=False)
    room_id = Column(Integer, ForeignKey('room.id'), nullable=False)

    def __init__(self, sensor, room, date, voltage):
        self.sensor = sensor
        self.room = room
        self.date = date
        self.voltage = voltage

    def __repr__(self):
        return '<Read {} {}>'.format(self.date, self.voltage)


if __name__ == '__main__':

    db = connect_db(
        username='pygg',
        password='albero della vita',
        db_name='pyggdrasill',
        echo=False)

    Base.metadata.create_all(db)
    S = sessionmaker(db)
    session = S()

    def mp_read(room, useless):
        sensor = Sensor('Pressure Sensor')

        return Room(sensor,
                    room,
                    datetime.now(),
                    20.0)

    #readings = sensor.read(room)
    pool = mpp.Pool()

    partial_read = partial(mp_read, useless=None)

    rooms = [Room('bedroom'), Room('Living'), Room('kitchen')]
    readings = pool.map(partial_read, rooms)

    # session.add(sensor)
    # session.commit()



-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to