Asynchronous Python Classes
I'm a real coding n00b so apologies if this is a simple or basic question.
I'm coding on Python, Webapp, Appengine.
My question, is it possible to carry on working after I've written out the page? And is that the best way to do something? Basically, when someone creates a list on my site (www.7bks.com) I want to carry on working for a little bit to do some 'post-processing' on the books they've just chosen.
Currently I have something like this (pseudocode!)
class InputList(webapp.RequestHandler):
def post(self):
#get books data from the post and put in datastore
list = List()
list = self.request.get('books')
list.put()
#redirect the user to the new list
self.redirect("http://www.7bks.com/list/&s" % list.id)
Now, I have some slow (involving API calls) post-processing I want to do to each book in the list. I don't want to slow down redirecting the user and generating the list page because my post-processing doesn't directly affect the list page. So could I do this?
class InputList(webapp.RequestHandler):
def post(self):
#get books data from the post and put in datastore
list = List()
list = self.request.get('books')
list.put()
#redirect the user to the new list
self.redirect("http://www.7bks.com/list/&s" % list.id)
#carry on working behind the scenes independently of the user
for book in list:
data = heavyprocessing(book)
Would that cause my application to effectively serve the redirect and then carry on working behind the scenes?
Are there b开发者_如何学JAVAetter ways of doing this? I'm aware I could use CRON but I'd like this heavy processed data fairly soon after I create the list, but not immediately. Feels like Cron might not be the right answer (unless I have a CRON script to run every minute or so and check for new books to process?)
I know it's not really async my question but I couldn't think of a good way to phrase it. I'm sure there's some standard terminology for this kind of thing but I don't know what it is. Thanks :)
Tom
Check out the Task Queue API.
Task or message queues tend to be the way to go about doing some sort of work that is initiated by a user request, but not necessarily completed within the time frame of that request's execution.
精彩评论