Riley Hughes
Riley Hughes

Reputation: 1484

running python application with asyncio async and await

I am trying to use asyncio and keywords await/async with python 3.5 I'm fairly new to asynchronous programming in python. Most of my experience with it has been with NodeJS. I seem to be doing everything right except for calling my startup function to initiate the program.

below is some fictitious code to water it down to where my confusion is because my code base is rather large and consists of several local modules.

import asyncio

async def get_data():
    foo = await <retrieve some data>
    return foo

async def run():
    await get_data()

run()

but I recieve this asyncio exception: runtimeWarning: coroutine 'run' was never awaited

I understand what this error is telling me but I'm confused as to how I am supposed to await a call to the function in order to run my program.

Upvotes: 0

Views: 841

Answers (1)

Mikhail Gerasimov
Mikhail Gerasimov

Reputation: 39606

You should create event loop manually and run coroutine in it, like shown in documentation:

import asyncio


async def hello_world():
    print("Hello World!")


loop = asyncio.get_event_loop()
loop.run_until_complete(hello_world())
loop.close()

Upvotes: 3

Related Questions