I have never worked with asyncio and/or asynchronous methods with django and am having a little difficulty understanding.
I am trying to convert a synchronous helper function (create_email_record) into an asynchronous function inside of a form method.
I will minimize some code for better understanding. my form method (begin_processing) has a good amount of logic inside.
def create_email_record(submission):
print("-----CREATE EMAIL RECORD-------")
creation = EmailRecord.objects.create()
class Form(forms.Form):
comments = forms.TextArea()
def begin_processing():
submission = Submission.objects.get(id=1)
print("----------BEGIN ASYNC--------")
create_email_notification = sync_to_async(create_email_record)(submission)
asyncio.run(create_email_notification)
print("----------END ASYNC----------")
When I print this to my console I would think to expect:
(“———-BEGIN ASYNC——–“)
(“———-END ASYNC———-“)
(“—–CREATE EMAIL RECORD——-“)
What I receive is:
(“———-BEGIN ASYNC——–“)
(“—–CREATE EMAIL RECORD——-“)
(“———-END ASYNC———-“)
My Object gets created, and seems to work, but I don’t believe that my converted sync_to_async function is being converted/called correctly.
Maybe I am having some trouble understanding, but what I want to do is call an asynchronous function from a synchronous function/method. I have read a lot on other posts, and online resources, but none seem to fit what I am looking to do. I have not seen this done inside of a form method.