For the current example, you need to have running loop somewhere (e.g. if you have some web-server or worker - loop.run_forever())
Fixed code example with running loop
import asyncio
async def callCoroutine(data):
print('This is data: %s' % data)
def lambda_handler(event, context):
loop = asyncio.get_event_loop()
task = loop.create_task(callCoroutine(context))
return
lambda_handler(None, {'a': 1})
loop = asyncio.get_event_loop()
loop.run_forever()
Example with run_in_executor()
import asyncio
import requests
def call_rest_api(data):
print('Triggered REST API in background')
response = requests.get(data['url'])
print('Response: %s' % response)
async def main(loop):
print('Some operations here...')
data = {'url': 'http://example.com#some_rest_api'}
loop.run_in_executor(None, call_rest_api, data)
print('Continue work of main thread...')
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(main(loop))
Example with simple await (if you need to call API syncronously)
But it is not necessary to use asyncio if you want to write synchronous code.
import asyncio
import requests
def call_rest_api(data):
print('Triggered REST API in background')
response = requests.get(data['url'])
print('Response: %s' % response)
async def main(loop):
print('Some operations here...')
data = {'url': 'http://example.com#some_rest_api'}
await call_rest_api(data)
print('Continue work of main thread...')
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(main(loop))
Can you provide more verbose example of what you have and what you want to achive?