[PonyORM-list] Running in thread ?

Alexander Kozlovsky alexander.kozlovsky at gmail.com
Sat Jul 26 23:18:27 UTC 2014


Hi Tom!

Yes, Pony ORM can be used from multiple thread, if (as in your example),
each thread uses its own db session. But SQLite in-memory database can have
only one connection. Because of this, SQLite in-memory database cannot be
used from multiple threads. Try to specify database file name, and all
should works as expected.

I think that Pony ORM should check if somebody tries to access SQLite
in-memory database from multiple threads, and generate appropriate error
message. I'll fix it in next release.

Regards,
Alexander


On Sunday, July 27, 2014, Tomislav Tustonic <tomislavtustonic at outlook.com>
wrote:

> Hello
>
> Is it possible to run a Pony ORM query in a different thread.
> In the following example the first call to 'printPersons' works fine, but
> the call from the thread throws an exception:
> OperationalError: no such table: Person
>
>
> from pony.orm import *
> import threading
>
> db = Database('sqlite', ':memory:')
> class Person(db.Entity):
>     name = Required(unicode)
>     age = Required(int)
> db.generate_mapping(create_tables=True)
> with db_session:
>     p1 = Person(name="Bing Bang", age="31")
>     p2 = Person(name="Kick Mick", age="75")
>
> def printPersons():
>     print "print persons: "
>     with db_session:
>         ps = Person.select(lambda p: True)
>         for p in ps:
>             print p.name
>
> printPersons()
> print "-------------------------"
> ppt = threading.Thread(target = printPersons)
> ppt.daemon = True
> ppt.start()
> a = raw_input('[Enter]')
>
>
>
>
> Thanks in advance,
> Tom
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: </ponyorm-list/attachments/20140727/e1286f6f/attachment.html>


More information about the ponyorm-list mailing list