1

I am writing a form submit in my application written in python/Django.Form has an attachment(upto 3MB) uploaded. On submit it has to save the attachment in aws s3, save the other data in database and also send emails. This form submit is taking too much time and the UI is hanging. Is there any other way to do this in python/django?

Indraja
  • 29
  • 1
  • 6

2 Answers2

0

The usual solution to tasks that are too long to be handled synchronously and can be handled asynchronously is to delegate them to some async queue like celery.

In your case, saving the form's data to db should be quite fast so I would not bother with this part, but moving the uploaded file to s3 and sending mails are good candidates.

bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118
  • Thank you. But is there anything light weight which can handle this? I wanted to try using threads but because of GIL in CPython I heard we do not get any use out of using threads. – Indraja Jan 08 '16 at 10:36
  • The GIL will prevent threads to take advantage of other available processors on a multiprocessor hardware. It doesn't mean there's no use for threads, but multiprocessing has much more benefits - like executing async processes on another server etc... – bruno desthuilliers Jan 08 '16 at 11:02
  • Thank you Bruno. I have tried using thread. Created two threads to do the tasks. These run in background and on frontend the UI is not hanging. – Indraja Jan 11 '16 at 09:29
  • If there is an exception in the method which the thread is executing? How to handle it. Will python terminate the thread. – Indraja Jan 11 '16 at 10:23
0

You have to profile your piece of code to understand what is the most time consuming.

If your upload to S3 is taking to much time, you can either have something asynchronous to avoid hanging your webpage or having a thread which will continue the upload in background and notify once completed.

If your insert into the DB is taking to much time, you can add a queue, eg. SQS to write your data once the database is less loaded. Of course this can be done only if your application don't rely on read consistency after write.

If your email sending is too long, you should consider using SES to avoid any backend latency issue.

Thomas L.
  • 1,294
  • 9
  • 13