https://pulumi.com logo
#python
Title
# python
f

full-artist-27215

10/25/2021, 5:28 PM
Has anyone managed to get async calls working inside a dynamic resource provider? I need to make a few HTTP calls, but can't quite sort out the magic to get Pulumi to work with them.
r

red-match-15116

10/25/2021, 5:36 PM
Can you say more about what isn’t working as you expect? Perhaps with code samples?
f

full-artist-27215

10/25/2021, 5:48 PM
I may be doing something wrong, as I've not had occasion to do async Python before, but it seems like I'd need something like this:
Copy code
async def create(self, inputs):
    let x = await make_an_http_request(...)
    # do stuff with x
    return CreateResult(...)
but I get
RuntimeWarning: coroutine 'create' was never awaited
r

red-match-15116

10/26/2021, 4:05 AM
You can make API calls in python without using async/await. Generally async/await is not used unless you are specifically using a library that uses asyncio.
You probably want to use something like requests which doesn’t use asyncio and just immediately awaits the output of the api call.
That being said if you do need to use an asyncio library, you’ll need to make sure that all asynchronous tasks are resolved within the
create
function, because that function itself cannot be
async
. So you’ll probably need to do something like:
Copy code
def create(self, inputs):
    x = asyncio.run(make_an_http_request(...))
    return CreateResult(...)
if
make_an_http_request
returns an Awaitable
f

full-artist-27215

10/26/2021, 3:49 PM
Ah, I see; thanks!
7 Views