List Info

Thread: Re: Cherrypy3 and SQLAlchemy




Re: Cherrypy3 and SQLAlchemy
country flaguser name
United States
2007-03-08 12:00:18
Clinton James wrote:
> I will be mounting several loosely coupled apps in
Cherrypy3.  I am 
> looking to use SQLAlchemy to handle object persistence

> (postgresql) due 
> to its ability to handle relations between tables and
the large 
> community surrounding the project.  My problem is
seeing how to 
> integrate SQLAlchemy into Cherrypy while still learning
both 
> frameworks. 
>   Here is what I think I have learned.
> 
> 1) I can use the global Metadata object for Table
creation.  
> This would 
> make the meta available to all threads.
> 
> 2) As the Session object is not thread safe, I must use
a 
> SessionContext 
> to instantiate Session objects on a per thread basis. 
This could be 
> used in the cherrypy.engine.on_start_thread_list.
> 
> Understandings?
> 
> As the sessions are now on a per thread basis, each are
separate. 
> Changes to an object are not reflected to an object in

> another thread's 
> session unless flush() is done in the first session and

> reload() in the 
> other. As more objects are stored in a thread local
session, each 
> session would contain duplicates of the other threads 
> session.  Object 
> identity would be a problem.
> 
> It would appear that it might be better to just create
a session on a 
> per request basis; using flush() and delete() at the
end of 
> the request. 
>   This would eliminate not only long term problems of
object identity 
> but the benefits of object identity for frequently
requested objects. 
> Are the two really incompatible?
> 
> Conclusion
> 
> At the moment it appears my only options is a per
request 
> session. While 
> the SQLAlchemy documentation is some of the best I have
ever seen, 
> figuring out how to use it with in Cherrypy escapes me.
 I am hoping 
> someone might have a comment on whether I am moving in
the right 
> direction or if I have missed a concept on using the
two 
> projects together.

One session per request is fine, and typical. The only other
option
would be to write a thread-safe cache between SA and your
code, using
any of several concurrency techniques: a Queue, lots of
locks (mutexes),
etc. But that's a lot of difficult work. ;)

If you go with the per-request session, the only hard part
is making
sure you flush at the right time. If you flush too late,
there's a
chance the browser will fetch page A' before you've flushed
(committed)
the changed data for page A. If you flush too early, you get
bad output.
;) See http://www.cherry
py.org/wiki/Dejavu for an example of how to
write a Tool to do this automatically.


Robert Brewer
System Architect
Amor Ministries
fumanchuamor.org

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "cherrypy-users" group.
To post to this group, send email to cherrypy-usersgooglegroups.com
To unsubscribe from this group, send email to
cherrypy-users-unsubscribegooglegroups.com
For more options, visit this group at h
ttp://groups.google.com/group/cherrypy-users?hl=en
-~----------~----~----~----~------~----~------~--~---


[1]

about | contact  Other archives ( Real Estate discussion Medical topics )