Admia
Admia

Reputation: 1144

Running Stripe Asynchronously

The following code takes few second to run:

payments = stripe.PaymentIntent.list(limit=10000)

How I can make the above code run asynchronously?

I tried await payments = stripe.PaymentIntent.list(limit=10000) but I received the error SyntaxError: cannot assign to await expression

Upvotes: 0

Views: 817

Answers (3)

xyres
xyres

Reputation: 21844

I've created a pure async (no threads) stripe client for Python: https://github.com/bhch/async-stripe/. It will give you better performance than threading.

Usage:

from async_stripe import stripe

stripe.api_key = '<your stripe api key>'

async myfunction():
    payments = await stripe.PaymentIntent.list(limit=10000)

Upvotes: 3

Nolan H
Nolan H

Reputation: 7459

You can kick it off without awaiting by calling an async function:

async function listPaymentIntents() {
  const payments = await stripe.PaymentIntent.list({limit: 10000});
  console.log('done!');
}

console.log('calling listPaymentIntents!');
listPaymentIntents();
console.log('called listPaymentIntents!');

And yes as @Barmar mentions the await goes on the value side to handle the promise resolution.

edit: Not an async python expert, but this seems to map to asyncio's concept of Tasks. Perhaps it could be done like so:

async def listPaymentIntents():
  payments = stripe.PaymentIntent.list(limit=10000);
  return payments

task = asyncio.create_task(listPaymentIntents())

// await task # optional

Upvotes: 0

Admia
Admia

Reputation: 1144

import time
import asyncio       
import threading


async def myfunction():

    await asyncio.sleep(10) #  sleep for 10 seconds

    payments = stripe.PaymentIntent.list(limit=10000)


server=threading.Thread(target=asyncio.run, args=(myfunction(),))

server.start()

Upvotes: 0

Related Questions