0

I am working on an application. I have a common scenario in which if payment is successful I need to notify the user on email. Basically, I am inside payment function, after successful payment, I have to notify user on email, I don't want to do it synchronously. for eg:

def process_payment():
    # some_payment_stuff
    notify_user_on_successful_payment()  # this should be asynchronously
    # some_payment_stuff
    return

I don't want process_payment to be dependent on notify_user_on_successful_payment success/failure.
process_payment() should get completed even if notify_user_on_successful_payment hasn't finished yet.

ywbaek
  • 2,971
  • 3
  • 9
  • 28
user1322495
  • 79
  • 1
  • 4

1 Answers1

0

Create the Email module with input parameters such as Amount, DateTime and TransactionID. Based on API used for the payment gateway, capture the response for the corresponding API. If the response code is success then use the condition to trigger the email module, else display a payment failed message.

you can achieve the asynchronous operation using multithreading. https://www.geeksforgeeks.org/multithreading-python-set-1/

But logically it wont make a difference as the separated thread needs execute first and join the main thread to complete the execution exit the main module.

Try these couple methods:

1) l.release() # lock.release() will release the parallel thread https://docs.python.org/3/library/multiprocessing.html#module-multiprocessing

  1. asyncio in How does asyncio actually work?
Avinash
  • 41
  • 3