Elixir - Threading issue

3 views
Skip to first unread message

yoav glazner

unread,
Jun 20, 2010, 9:10:32 AM6/20/10
to sqle...@googlegroups.com
Hi,

I have a problem in my program, i can replacte it this way:
<code>
from elixir import *
class Job(Entity):
  number = Field(Integer)

def f(job):
  job.expire()
  job.number

metadata.bind = "oracle://user:password@db"
setup_all()
job = Job.query.get(1) #number is primary key
thread.start_new(f,(job,))
thread.start_new(f,(job,))

time.sleep(4)
</code>

ok now i get this:
AttributeError: expired_attributes

how should i approch this?
(I know that if i put a lock on function "f" it doesn't happen...)

Many Thanks,

Yoav Glazner



Diez B. Roggisch

unread,
Jun 20, 2010, 1:17:27 PM6/20/10
to sqle...@googlegroups.com

Please post the full traceback.

Diez

yoav glazner

unread,
Jun 21, 2010, 4:27:04 AM6/21/10
to sqle...@googlegroups.com
Please post the full traceback.

Unhandled exception in thread started by <function f at 0x01049A30>
Traceback (most recent call last):
  File "C:\Test\src\elxOra.py", line 21, in f
    job.number
  File "C:\Python26\lib\site-packages\sqlalchemy-0.5.4p2-py2.6.egg\sqlalchemy\orm\attributes.py", line 158, in __get__
    return self.impl.get(instance_state(instance), instance_dict(instance))
  File "C:\Python26\lib\site-packages\sqlalchemy-0.5.4p2-py2.6.egg\sqlalchemy\orm\attributes.py", line 374, in get
    value = callable_()
  File "C:\Python26\lib\site-packages\sqlalchemy-0.5.4p2-py2.6.egg\sqlalchemy\orm\state.py", line 175, in __call__
    del self.expired_attributes
AttributeError: expired_attributes 

Gaetan de Menten

unread,
Jun 21, 2010, 7:48:28 AM6/21/10
to sqle...@googlegroups.com

As far as I know, you shouldn't share a particular object instance
between several threads.

The answer to the question: "Is the session thread-safe?" in:

http://www.sqlalchemy.org/docs/session.html#frequently-asked-questions

applies to your case. But you can perfectly load the same row in two
different threads... If both threads can write, you'll have to
implement some kind of synchronisation mechanism though.

--
Gaëtan de Menten

yoav glazner

unread,
Jun 21, 2010, 1:13:36 PM6/21/10
to sqle...@googlegroups.com
If one thread can only read then this should be safe?
def f(jobNumber):
   job = Job.get(jobNumber)
   job.expire()
   job.number

(f is a threaded function ...)
I can't test this now since i don't have a DB here =(

Diez B. Roggisch

unread,
Jun 21, 2010, 2:13:07 PM6/21/10
to sqle...@googlegroups.com

Looks good, yes.

Diez

yoav glazner

unread,
Jun 21, 2010, 4:25:51 PM6/21/10
to sqle...@googlegroups.com

Looks good, yes.

Stop me if I'm asking too much, but.
will this work because:
 job = Job.get(jobNumber) #will be a newly created object, not the one from the identity-map

Thank you and Gaëtan for the answers

yoav glazner

unread,
Jun 22, 2010, 9:34:08 AM6/22/10
to sqle...@googlegroups.com


On Mon, Jun 21, 2010 at 11:25 PM, yoav glazner <yoavg...@gmail.com> wrote:

Looks good, yes.

Stop me if I'm asking too much, but.
will this work because:
 job = Job.get(jobNumber) #will be a newly created object, not the one from the identity-map

ok, checked it myself, it seems it brings a fresh object since if i do id(job) it brigs a new address.
Reply all
Reply to author
Forward
0 new messages