Return a http response before actual function is completed
I have a for开发者_如何转开发m on my site that uploads a file. Currently i upload the file to amazon S3 before sending a response back to the user. Is there a way in Django to send a "Success" response to the user immediately after the file is saved on my local server, but before I i start saving it to s3?
You should use a distributed task queue.
Since you are using AWS already, you might want to consider their alternative: Amazon SQS.
I'm actually doing something similar, to do it I'm using the python multiprocessing library to handle the file uploading to s3. So once the I receive the request, I start up the file handling process and then instantly return a success response back to the user. Depending on how much server load you plan on experiencing it may be better for you to handle the file uploading to s3 using a queueing system, but for a site thats starting out or has low hits it should be fine.
I believe the best practice here would be to add the file (path, info) to a queue, for moving to S3 in a later process. Accept the file, send the success message, and "send it to the cloud" later. Something run by cron, or some other regularly occurring "task queue" type situation.
This, of course, depends on exactly what you're doing with the file, and some more context.
精彩评论