sqlalchemy의 업데이트 수동commit 이전에 yield를 통해 원격 비동기 호출을 실행할 때session 대상이 바뀌었습니다

1837 단어 python
SQLAlchemy==1.2.18
result = db.session.query(ObjectModel).get(ids.get("id"))
result.status = 2   #   session._is_clean()  =  False

#      yield    
#      
def onAyncFetch():
    req = ...      
    response = yield async_http.fetch(req)
    ...      
    return Return(json_decode(response.body))
print id(result.query.session) 
data = yield onAyncFetch(...)
print id(result.query.session) #           
...      
#  yield       session._is_clean()  =  True

db.session.commit() #   commit         ,     status     commit

위쪽은 위조 코드입니다.절차는 대략 이렇다. 아래에 sqlalchemy를 붙일 때commiit의 원본 코드를 붙인다
sqlalchemy/orm/session.py

    def _prepare_impl(self):
        self._assert_active()
        if self._parent is None or self.nested:
            self.session.dispatch.before_commit(self.session)

        stx = self.session.transaction
        if stx is not self:
            for subtransaction in stx._iterate_self_and_parents(upto=self):
                subtransaction.commit()

        if not self.session._flushing:
            for _flush_guard in range(100):
                if self.session._is_clean():  #           ,       flush()
                    break
                self.session.flush()
            else:
                raise exc.FlushError(
                    "Over 100 subsequent flushes have occurred within "
                    "session.commit() - is an after_flush() hook "
                    "creating new objects?"
                )

        if self._parent is None and self.session.twophase:
            try:
                for t in set(self._connections.values()):
                    t[1].prepare()
            except:
                with util.safe_reraise():
                    self.rollback()

        self._state = PREPARED

좋은 웹페이지 즐겨찾기