I am using the discord.py to interact with discord.
def hi():
await client.send_message(channel, message)
gets a syntax error, unless I write "async" when I the hi() function. I really don't understand this, pls help!
You can schedule a coroutine in an event loop. You can not call them or await them from inside a non coroutine.
Let's check the following code:
import asyncio
async def greetLater(name, delay):
await asyncio.sleep(delay)
print("Hello {}!".format(name))
def main():
loop = asyncio.get_event_loop()
loop.run_until_complete(greetLater("masnun", 10))
if __name__ == '__main__':
main()
In the above code, greetLater is a coroutine. We can't directly call it from the main function. What we need to do here is to create an event loop and then schedule the coroutine there. We use the asyncio module to help us create the event loop and run the coroutine.
Further references:
http://masnun.com/2015/11/13/python-generators-coroutines-native-coroutines-and-async-await.html
http://masnun.com/2015/11/20/python-asyncio-future-task-and-the-event-loop.html
(Disclaimer: Links to my personal blog posts on the topic, you can google for more references)