运行 异步条带化
Running Stripe Asynchronously
以下代码需要几秒钟的时间才能 运行:
payments = stripe.PaymentIntent.list(limit=10000)
如何使上述代码 运行 异步?
我试过 await payments = stripe.PaymentIntent.list(limit=10000)
但我收到错误 SyntaxError: cannot assign to await expression
您可以通过调用异步函数在不等待的情况下启动它:
async function listPaymentIntents() {
const payments = await stripe.PaymentIntent.list({limit: 10000});
console.log('done!');
}
console.log('calling listPaymentIntents!');
listPaymentIntents();
console.log('called listPaymentIntents!');
是的,正如@Barmar 提到的,await
在价值方面处理承诺决议。
编辑:不是异步 python 专家,但这似乎映射到异步 Tasks 的概念。也许可以这样做:
async def listPaymentIntents():
payments = stripe.PaymentIntent.list(limit=10000);
return payments
task = asyncio.create_task(listPaymentIntents())
// await task # optional
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()
以下代码需要几秒钟的时间才能 运行:
payments = stripe.PaymentIntent.list(limit=10000)
如何使上述代码 运行 异步?
我试过 await payments = stripe.PaymentIntent.list(limit=10000)
但我收到错误 SyntaxError: cannot assign to await expression
您可以通过调用异步函数在不等待的情况下启动它:
async function listPaymentIntents() {
const payments = await stripe.PaymentIntent.list({limit: 10000});
console.log('done!');
}
console.log('calling listPaymentIntents!');
listPaymentIntents();
console.log('called listPaymentIntents!');
是的,正如@Barmar 提到的,await
在价值方面处理承诺决议。
编辑:不是异步 python 专家,但这似乎映射到异步 Tasks 的概念。也许可以这样做:
async def listPaymentIntents():
payments = stripe.PaymentIntent.list(limit=10000);
return payments
task = asyncio.create_task(listPaymentIntents())
// await task # optional
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()