最新消息:20190717 VPS服务器:Vultr新加坡,WordPress主题:大前端D8,统一介绍入口:关于

[已解决]Flask-RQ2中执行后台调度任务出错:DetachedInstanceError Parent instance Event at is not bound to a Session

Flask crifan 2192浏览 0评论

Flask的代码:

/Users/crifan/dev/dev_root/daryun/SIPEvents/sourcecode/sipevents/sipevents/models.py

class User(db.Model):
    __tablename__ = ‘wechat_users’
    # Columns
   。。。
    events = db.relationship(‘Event’, backref = ‘creator’, lazy = ‘dynamic’)
class Event(db.Model):
    __tablename__ = “events”
    # Columns
    id = db.Column(db.Integer, primary_key = True, autoincrement = True, nullable=False)

/Users/crifan/dev/dev_root/daryun/SIPEvents/sourcecode/sipevents/sipevents/views.py

@rq.job
# @login_required
def notifyEvent(eventToNotif, notifDatetimeGmt):
    gLog.debug(“eventToNotif=%s, notifDatetimeGmt=%s”, eventToNotif, notifDatetimeGmt)
    # curUser = g.user
    # gLog.debug(“curUser=%s”, curUser)
    eventCreator = eventToNotif.creator
    gLog.debug(“eventCreator=%s”, eventCreator)
       db.session.add(newEvent)
        gLog.debug(“before flush newEvent=%s”, newEvent)
        # db.session.refresh(newEvent)
        db.session.flush()
        gLog.debug(“after  flush newEvent=%s”, newEvent)
        db.session.commit()
        gLog.debug(“added new event=%s”, newEvent)
        gLog.debug(“try add background work to notify for event”)
        if notificationTimeEnum == NotificationTime.NotNotify:
            gLog.debug(“no set notify time for event=%s”, newEvent)
        else:
            timedeltaAfter = timedelta(seconds=0)
            gLog.debug(“timedeltaAfter=%s”, timedeltaAfter)
            if notificationTimeEnum == NotificationTime.WhenEventHappen:
                timedeltaAfter = timedelta(seconds=0)
            elif notificationTimeEnum == NotificationTime.FiveMinutesBefore:
                timedeltaAfter = timedelta(minutes=5)
            elif notificationTimeEnum == NotificationTime.FiftyMinutesBefore:
                timedeltaAfter = timedelta(minutes=15)
            elif notificationTimeEnum == NotificationTime.ThirtyMinutesBefore:
                timedeltaAfter = timedelta(minutes=30)
            elif notificationTimeEnum == NotificationTime.OneHourBefore:
                timedeltaAfter = timedelta(hours=1)
            elif notificationTimeEnum == NotificationTime.TwoHoursBefore:
                timedeltaAfter = timedelta(hours=2)
            elif notificationTimeEnum == NotificationTime.OneDayBefore:
                timedeltaAfter = timedelta(days=1)
            elif notificationTimeEnum == NotificationTime.TwoDaysBefore:
                timedeltaAfter = timedelta(days=2)
            elif notificationTimeEnum == NotificationTime.OneWeekBefore:
                timedeltaAfter = timedelta(weeks=1)
            gLog.debug(“timedeltaAfter=%s”, timedeltaAfter)
            gLog.debug(“startDate=%s”, startDate)
            notificationDatetime = startDate + timedeltaAfter
            gLog.debug(“notificationDatetime=%s”, notificationDatetime)
            gmtNotificationDatetime = crifanLib.convertLocalToGmt(notificationDatetime)
            gLog.debug(“gmtNotificationDatetime=%s”, gmtNotificationDatetime)
            # schedule job at specific notification time
            notificationJob = notifyEvent.schedule(
                gmtNotificationDatetime,
                eventToNotif=newEvent,
                notifDatetimeGmt=gmtNotificationDatetime)
            gLog.debug(“notificationJob=%s”, notificationJob)

出错:

<span style="color: rgb(248, 248, 242); font-family: Menlo; font-size: 9pt;"–<——————————————————————————

22:14:29 DetachedInstanceError: Parent instance <Event at 0x7fa7bcc9d390> is not bound to a Session; lazy load operation of attribute ‘creator’ cannot proceed

Traceback (most recent call last):

  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/rq/worker.py”, line 588, in perform_job

    rv = job.perform()

  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/rq/job.py”, line 498, in perform

    self._result = self.func(*self.args, **self.kwargs)

  File “./sipevents/views.py”, line 376, in notifyEvent

    eventCreator = eventToNotif.creator

  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/orm/attributes.py”, line 237, in __get__

    return self.impl.get(instance_state(instance), dict_)

  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/orm/attributes.py”, line 584, in get

    value = self.callable_(state, passive)

  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/orm/strategies.py”, line 544, in _load_for_state

    (orm_util.state_str(state), self.key)

DetachedInstanceError: Parent instance <Event at 0x7fa7bcc9d390> is not bound to a Session; lazy load operation of attribute ‘creator’ cannot proceed

Traceback (most recent call last):

  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/rq/worker.py”, line 588, in perform_job

    rv = job.perform()

  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/rq/job.py”, line 498, in perform

    self._result = self.func(*self.args, **self.kwargs)

  File “./sipevents/views.py”, line 376, in notifyEvent

    eventCreator = eventToNotif.creator

  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/orm/attributes.py”, line 237, in __get__

    return self.impl.get(instance_state(instance), dict_)

  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/orm/attributes.py”, line 584, in get

    value = self.callable_(state, passive)

  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/orm/strategies.py”, line 544, in _load_for_state

    (orm_util.state_str(state), self.key)

DetachedInstanceError: Parent instance <Event at 0x7fa7bcc9d390> is not bound to a Session; lazy load operation of attribute ‘creator’ cannot proceed

22:14:29 Moving job to u’failed’ queue

22:14:29 

22:14:29 *** Listening on default…

搜:

DetachedInstanceError: Parent instance  is not bound to a Session; lazy load operation of attribute ‘creator’ cannot proceed

python – Parent instance is not bound to a Session; lazy load operation of attribute ’account’ cannot proceed – Stack Overflow

python – {DetachedInstanceError} Parent instance <Car> is not bound to a session; lazy load operation of attribute ‘owner’ cannot proceed – Stack Overflow

[python] 解决 SQLAlchemy 提示 Instance is not bound to a Session 错误的问题 – Mozillazg’s Blog

    #events = db.relationship(‘Event’, backref = ‘creator’, lazy = ‘dynamic’)
    events = db.relationship(‘Event’, backref=’creator’, lazy=’subquery’)

结果:

还是同样错误:

DEBUG in views [./sipevents/views.py:371]:

eventToNotif=<Event id=29 user_openid=oswjmv4X0cCXcfkIwjoDfCkeTVVY title=u’subquery’>, notifDatetimeGmt=2016-09-04 14:31:00

——————————————————————————–

22:31:02 DetachedInstanceError: Parent instance <Event at 0x7fa7bcc9d390> is not bound to a Session; lazy load operation of attribute ‘creator’ cannot proceed

Traceback (most recent call last):

  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/rq/worker.py”, line 588, in perform_job

    rv = job.perform()

  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/rq/job.py”, line 498, in perform

    self._result = self.func(*self.args, **self.kwargs)

  File “./sipevents/views.py”, line 376, in notifyEvent

    eventCreator = eventToNotif.creator

  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/orm/attributes.py”, line 237, in __get__

    return self.impl.get(instance_state(instance), dict_)

  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/orm/attributes.py”, line 584, in get

    value = self.callable_(state, passive)

  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/orm/strategies.py”, line 544, in _load_for_state

    (orm_util.state_str(state), self.key)

DetachedInstanceError: Parent instance <Event at 0x7fa7bcc9d390> is not bound to a Session; lazy load operation of attribute ‘creator’ cannot proceed

Traceback (most recent call last):

  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/rq/worker.py”, line 588, in perform_job

    rv = job.perform()

  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/rq/job.py”, line 498, in perform

    self._result = self.func(*self.args, **self.kwargs)

  File “./sipevents/views.py”, line 376, in notifyEvent

    eventCreator = eventToNotif.creator

  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/orm/attributes.py”, line 237, in __get__

    return self.impl.get(instance_state(instance), dict_)

  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/orm/attributes.py”, line 584, in get

    value = self.callable_(state, passive)

  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/orm/strategies.py”, line 544, in _load_for_state

    (orm_util.state_str(state), self.key)

DetachedInstanceError: Parent instance <Event at 0x7fa7bcc9d390> is not bound to a Session; lazy load operation of attribute ‘creator’ cannot proceed

22:31:02 Moving job to u’failed’ queue

22:31:02 

22:31:02 *** Listening on default…

算了:

还是之传入对应的event的id,然后后续再去获取event,

应该就可以了。

@rq.job
# @login_required
def notifyEvent(eventId, notifDatetimeGmt):
    eventToNotif = getEventInfo(eventId)
    gLog.debug(“eventId=%s, eventToNotif=%s, notifDatetimeGmt=%s”, eventId, eventToNotif, notifDatetimeGmt)
    eventCreator = eventToNotif.creator
    gLog.debug(“eventCreator=%s”, eventCreator)
            # schedule job at specific notification time
            notificationJob = notifyEvent.schedule(
                gmtNotificationDatetime,
                #eventToNotif=newEvent,
                eventId = newEvent.id,
                notifDatetimeGmt=gmtNotificationDatetime)
            gLog.debug(“notificationJob=%s”, notificationJob)

然后就可以了,可以获得对应的数据了:

DEBUG in views [./sipevents/views.py:375]:
eventCreator=<User nickname=u’\u793c\u8c8c’ openid=oswjmv4X0cCXcfkIwjoDfCkeTVVY avatar_static_path=img/avatar/oswjmv4X0cCXcfkIwjoDfCkeTVVY.png>

转载请注明:在路上 » [已解决]Flask-RQ2中执行后台调度任务出错:DetachedInstanceError Parent instance Event at is not bound to a Session

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
21 queries in 0.112 seconds, using 9.47MB memory