Python Asynchronous Classes

I am real n00b code, so I apologize if this is a simple or basic question.

I code Python, Webapp, Appengine.

My question is, can I continue to work after I wrote out the page? And is this the best way to do something? Basically, when someone creates a list on my website (www.7bks.com), I want to work a bit to do some “post-processing” in the books that they just selected.

I currently have something like this (pseudo code!)

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 a slow (with an API call) post-processing that I want to do for each book in the list. I don’t want to slow down the redirection of the user and the creation of the list page, because my post processing does not directly affect the list page. Can I do it?

 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) 

Is this the reason that my application efficiently serves redirection and then continues to work behind the scenes?

Are there any better ways to do this? I know that I can use CRON, but I would like this heavy processed data to be pretty soon after creating the list, but not right away. Feelings like Cron might be the wrong answer (unless I have a CRON script to run every minute or so) and check that new books are processed?)

I know that this is not very asynchronous, but I could not figure out how to express it correctly. I am sure there is such a standard terminology for this kind of thing, but I do not know what it is. Thanks:)

Tom

+4
source share
1 answer

Check out the task queue API .

Message targets or queues are typically a way of doing some kind of work initiated by a user request, but not necessarily completed during the execution period of this request.

+5
source

Source: https://habr.com/ru/post/1334561/


All Articles