one issue is that youre creating an endless number of threads using a  
"while True"...a python interpreter (and your computer) can only  
handle a limited number of simultaneous threads.

the other is that the session object is not threadsafe.  youll need  
to use a distinct session per each thread.

the "threadlocal" mod and more generally the "sessioncontext" module  
can help with this task; but those should not be used unless you  
understand what theyre doing.

in all cases, with or without one of those helper extensions, you  
dont want to have the "create_session" step at the module level like  
that.

On Sep 18, 2006, at 5:39 PM, Viktor wrote:

> Probably I'm missing something about SQLAlchemy used in threading
> environment, but I can't make this code work whatever I do:
>
>
> import sqlalchemy as sa
> import random
>
> db = sa.create_engine('postgres://alef:[EMAIL PROTECTED]:5432/test')
> metadata = sa.BoundMetaData(db)
>
> session = sa.create_session(bind_to=db)
>
> customer_table = sa.Table('customer', metadata,
>      sa.Column('customerid', sa.Integer, primary_key=True),
>      sa.Column('name', sa.String()),
>      sa.Column('age', sa.Integer))
>
>
> class Customer(object):
>      pass
>
>
> Customer._mapper = sa.mapper(Customer, customer_table)
> Customer.objects = session.query(Customer)
>
>
>
> def worker():
>      c = Customer()
>      c.name = str(random.randint(0, 100))
>      c.age  = random.randint(0, 100)
>
>      session.save(c)
>      session.flush()
>
>      c = Customer.objects.select_by(name=str(random.randint(0, 100)),
> age=random.randint(0, 100))
>      print c
>
> if __name__ == '__main__':
>      while True:
>          t = threading.Thread(target=worker)
>          t.setDaemon(1)
>          t.start()
>
>
> I tried adding threadlocks, using threadlocal, but whatever I use,  
> I get
> some kind of exception...
>
> Thanks in advance.
>
> ---------------------------------------------------------------------- 
> ---
> Take Surveys. Earn Cash. Influence the Future of IT
> Join SourceForge.net's Techsay panel and you'll get the chance to  
> share your
> opinions on IT & business topics through brief surveys -- and earn  
> cash
> http://www.techsay.com/default.php? 
> page=join.php&p=sourceforge&CID=DEVDEV
> _______________________________________________
> Sqlalchemy-users mailing list
> Sqlalchemy-users@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users


-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Sqlalchemy-users mailing list
Sqlalchemy-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users

Reply via email to