[sqlalchemy] Passing model objects to other threads

2010-10-01 Thread James
Hi all,
i'm using SQLAlchemy in a TG2 web app which has long-running jobs
kicked off by user POSTs.

I'm planning on writing the pending jobs to the DB, then kick off a
threading.Thread at the end of the web app method to actually do the
work.

Question - I'll need to pass the job description (a SA object) to the
child thread; but to use the description, I need to detach it from the
old TG2 thread's SA session, and reattach it to the child thread's SA
session, right?

How best to do that? I don't see a way of getting the current session
from a live SA object...

Thanks!
James

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@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.



Re: [sqlalchemy] Passing model objects to other threads

2010-10-01 Thread Michael Bayer

On Oct 1, 2010, at 9:40 AM, James wrote:

 Hi all,
 i'm using SQLAlchemy in a TG2 web app which has long-running jobs
 kicked off by user POSTs.
 
 I'm planning on writing the pending jobs to the DB, then kick off a
 threading.Thread at the end of the web app method to actually do the
 work.
 
 Question - I'll need to pass the job description (a SA object) to the
 child thread; but to use the description, I need to detach it from the
 old TG2 thread's SA session, and reattach it to the child thread's SA
 session, right?
 
 How best to do that? I don't see a way of getting the current session
 from a live SA object...

If you're writing jobs to the DB anyway, why not use a different process to 
handle the jobs, and have it poll the jobs table, instead of passing directly ? 
  That's how I'm currently doing it.  The job runner then forks off additional 
procs to run N number of jobs simultaneously with multiprocessing.   You at 
least get true concurrency that way.

Otherwise, the general paradigm to pass state between threads uses merge(), it 
leaves the original unaffected.

obj = Session.query(...).first()

start_new_thread(obj)

# in the thread's method:

def start_my_thing(obj):
   obj = Session.merge(obj)


-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@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.