0

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.

1 Answer 1

3

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()
Sign up to request clarification or add additional context in comments.

2 Comments

This makes so much sense. I misunderstood the loop concept. My code is inside of a main event loop but I need to set an asyncio event loop.
@RileyHughes just to be clear: unlike in NodeJS in Python there's no any kind of default interpreter-level event loop. Event loop in code above is a part of asyncio module: you need to set up it to run asyncio coroutines in it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.