Skip to content

Instantly share code, notes, and snippets.

@asolera
Created January 26, 2021 18:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asolera/18b42b462f7f052ac78ac78fa43241a0 to your computer and use it in GitHub Desktop.
Save asolera/18b42b462f7f052ac78ac78fa43241a0 to your computer and use it in GitHub Desktop.
Airflow 2.0.0 Async HTTP Request with aiohttp DAG example
import aiohttp
import asyncio
from airflow.decorators import dag, task
from airflow.operators.python import task
from datetime import date, datetime, timedelta
async def async_main():
async with aiohttp.ClientSession() as session:
async with session.get('http://httpbin.org/get') as resp:
print(resp.status)
print(await resp.text())
default_args = {
'owner': 'Airflow',
'retries': 0
}
@dag('AsyncExample', default_args=default_args, schedule_interval='@once', start_date=datetime(2021, 1, 26), catchup=False)
def async_dag():
@task()
def main() -> None:
try:
loop = asyncio.get_event_loop()
loop.run_until_complete(async_main())
except Exception as e:
print(e)
finally:
loop.close()
main()
dag = async_dag()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment