heres my script, runs fine

from sqlalchemy import *
from sqlalchemy.orm import *

e = create_engine('postgres://scott:ti...@localhost/test', echo=True)
m = MetaData(e)
t1 = Table('t1', m, Column('a', Integer, primary_key=True),  
Column('b', Integer))
t2 = Table('t2', m, Column('a', Integer, primary_key=True),  
Column('arefid', Integer, ForeignKey('t1.a')))

class A(object):
     def __init__(self, a, b):
         self.a = a
         self.b = b

class B(object):
     def __init__(self, b):
         self.aref = b

mapper(A, t1)
mapper(B, t2, properties={
     'aref':relation(A)
})

m.create_all()

Session = scoped_session(sessionmaker())

Session.add(A(1, 1))
Session.commit()

import threading

def insertalarm():
     a = Session.query(A).get(1)
     b = B(a)
     Session.add(b)
     Session.commit()

for i in range(100):
    t=threading.Thread(target=insertalarm)
    t.start()

it doesnt run 100 concurrent threads since the inserts happen more  
quickly than the threads can be started.




On Aug 2, 2009, at 10:28 PM, drakkan wrote:

>
> and here is the django equivalent that works as expeted:
>
> def insertalarmdjango():
>        print 'qui'
>        a=Allarmi()
>        a.tipoallarme=TipAllarmi.objects.get(pk=1)
>        a.save()
>
> for i in range(100):
>    t=threading.Thread(target=insertalarmdjango)
>    t.start()
>
>
> On 3 Ago, 04:21, drakkan <drakkan1...@gmail.com> wrote:
>> The problem is riproducible with this simple script:
>>
>> import samodels as sa
>> import threading
>>
>> def insertalarm():
>>         s=sa.Session
>>         a=sa.Allarmi(s.query(sa.TipoAllarmi).get(1))
>>         s.add(a)
>>         s.commit()
>>
>> for i in range(100):
>>     t=threading.Thread(target=insertalarm)
>>     t.start()
>>
>> with range(1) works as expect, if you change the range the script  
>> hang
>> or give errors, I'm using scoped_session as you suggested
>>
>> On 3 Ago, 03:34, drakkan <drakkan1...@gmail.com> wrote:
>>
>>> After the select there is an insert, can three concurrent threads
>>> inserting data in the same table cause the hang?
>>
>>> If so how can I avoid this?
>>
>>> Please note that until now the same application was using django orm
>>> with no deadlock problems, I only changed the query to use  
>>> sqlalchemy
>>
>>> thanks
>>> drakkan
>>
>>> On 3 Ago, 03:20, Michael Bayer <mike...@zzzcomputing.com> wrote:
>>
>>>> On Aug 2, 2009, at 9:02 PM, drakkan wrote:
>>
>>>>> here is a sample sa output when the application hangs:
>>
>>>>> 009-08-03 01:05:20,458 INFO sqlalchemy.engine.base.Engine.0x... 
>>>>> 1634
>>>>> {'param_1': None}
>>>>> 2009-08-03 01:05:33,673 INFO sqlalchemy.engine.base.Engine.0x... 
>>>>> 1634
>>>>> SELECT tipallarmi.id AS tipallarmi_id, tipallarmi.codice AS
>>>>> tipallarmi_codice, tipallarmi.tipo AS tipallarmi_tipo
>>>>> FROM tipallarmi
>>>>> WHERE tipallarmi.codice = %(codice_1)s
>>>>> LIMIT 1 OFFSET 0
>>>>> 2009-08-03 01:05:33,681 INFO sqlalchemy.engine.base.Engine.0x... 
>>>>> 1634
>>>>> {'codice_1': 1}
>>>>> 2009-08-03 01:05:33,679 INFO sqlalchemy.engine.base.Engine.0x... 
>>>>> 1634
>>>>> SELECT tipallarmi.id AS tipallarmi_id, tipallarmi.codice AS
>>>>> tipallarmi_codice, tipallarmi.tipo AS tipallarmi_tipo
>>>>> FROM tipallarmi
>>>>> WHERE tipallarmi.codice = %(codice_1)s
>>>>> LIMIT 1 OFFSET 0
>>>>> 2009-08-03 01:05:33,681 INFO sqlalchemy.engine.base.Engine.0x... 
>>>>> 1634
>>>>> {'codice_1': 1}
>>>>> 2009-08-03 01:05:33,680 INFO sqlalchemy.engine.base.Engine.0x... 
>>>>> 1634
>>>>> SELECT tipallarmi.id AS tipallarmi_id, tipallarmi.codice AS
>>>>> tipallarmi_codice, tipallarmi.tipo AS tipallarmi_tipo
>>>>> FROM tipallarmi
>>>>> WHERE tipallarmi.codice = %(codice_1)s
>>>>> LIMIT 1 OFFSET 0
>>>>> 2009-08-03 01:05:33,682 INFO sqlalchemy.engine.base.Engine.0x... 
>>>>> 1634
>>>>> {'codice_1': 1}
>>
>>>>> seems there are three concurrent threads making the same query
>>
>>>> if you have three threads all calling query.get() then that would  
>>>> be
>>>> the result.
>>
>>>> none of the SELECTs should be causing deadlocks so something else  
>>>> is
>>>> causing it to hang.
> >


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to