Jordan Rose
Jordan Rose

Reputation: 300

Running model.save() method asynchronously?

I am wondering if it is possible to run a save method for a model that I've overwritten asynchronously? If so how would you implement it?

Upvotes: 0

Views: 551

Answers (2)

aliva
aliva

Reputation: 5720

for web development you have to return a response quickly and using threads or multiprocessing won't help (user will reach timeout and your task will fail

the solution is writing a background task runner (using something like celery)

when user sends a request you send a task call to celery and invoke the background task, the task will run in another process (creating those rows) and you can return a response to user saying your request is being processed.

Upvotes: 1

modesitt
modesitt

Reputation: 7210

why not use multiprocessing

from multiprocessing import Pool

pool = Pool(processes=1)
result = pool.apply_async(model.save)

to spawn a new process to save your model. you can pass whatever parameters like a path using other arguments in apply_async aswell.

Upvotes: 0

Related Questions