Answers for "django async"

6

python async await

import asyncio

async def print_B(): #Simple async def
    print("B")

async def main_def():
    print("A")
    await asyncio.gather(print_B())
    print("C")
asyncio.run(main_def())

# The function you wait for must include async
# The function you use await must include async
# The function you use await must run by asyncio.run(THE_FUNC())
Posted by: Guest on February-04-2021
1

python async await

import asyncio
from PIL import Image
import urllib.request as urllib2

async def getPic(): #Proof of async def
    pic = Image.open(urllib2.urlopen("https://c.files.bbci.co.uk/E9DF/production/_96317895_gettyimages-164067218.jpg"))
    return pic

async def main_def():
    print("A")
    print("Must await before get pic0...")
    pic0 = await asyncio.gather(getPic())
    print(pic0)
asyncio.run(main_def())
Posted by: Guest on February-04-2021
0

async django

#pip install django-background-tasks

from background_task import background

# schedule = 60 second * 5  : Task will perform after 300 second  

@background(schedule=60*5)
def send_html_mail_post(id, template):
    u = User.objects.get(id=id)
    user_email = u.email
    subject = "anything"
    html_content = template.format(arguments)
    from_email, to = from_email, user_email
    text_content = ''
    msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
    msg.attach_alternative(html_content, "text/html")
    msg.send()
    
    
send_html_mail(1,'Hello this is the test email')
Posted by: Guest on August-16-2021

Python Answers by Framework

Browse Popular Code Answers by Language