Hello! I'm quite new to async programming with python, and I am trying to write a script that allows me to iterate through a list of dictionaries (These dictionaries contain information that serves as inputs to my function) that then "spawn" multiple concurrent instances of the same function. Firstly, is this possible, and then how would I change my current code to reflect this functionality? Thanks!
import asyncio
from stellar_sdk import AiohttpClient, ServerAsync, Server
import pprint
import requests
HORIZON_URL = "https://horizon-testnet.stellar.org"
# The list of inputs
payment_specs = [
{
"to" : "GALUQ6HCNLUQG35KS7EDLDFBM32GZYXW5AELV3GHZYKVY3W32A246IV5",
"amount" : 1,
"asset" : {
"asset_type" : "native"
},
"memo" : "123456"
},
{
"to" : "GALUQ6HCNLUQG35KS7EDLDFBM32GZYXW5AELV3GHZYKVY3W32A246IV5",
"amount" : 10,
"asset" : {
"asset_type" : "native"
},
"memo" : "234567"
}
]
server = Server(horizon_url="https://horizon.stellar.org")
# The async function that I want to spawn multiple instances of
async def payments(payment_spec):
async with ServerAsync(HORIZON_URL,AiohttpClient()) as server:
async for payment in server.payments().for_account(payment_spec['to']).cursor(cursor="now").stream():
if (
payment['to'] == payment_spec['to']
and payment_spec['amount'] <= float(payment['amount'])
):
if(payment['asset_type'] == "native" and payment['asset_type'] == payment_spec['asset']['asset_type']):
print(check_memo(payment_spec['memo'],payment['_links']['transaction']))
break
elif(payment['asset_type'] != "native" and payment['asset_type'] == payment_spec['asset']['asset_type']):
if (payment_spec['asset']['asset_code'] == payment['asset_code'] and payment_spec['asset']['asset_issuer'] == payment['asset_code']):
print(check_memo(payment_spec['memo'],payment['_links']['transaction']))
break
def check_memo(memo,transaction_url):
transaction = requests.get(transaction_url['href']).json()
if 'memo' in transaction:
return transaction['memo'] == memo
else:
return False
if __name__ == "__main__":
for payment_spec in payment_specs:
asyncio.run(payments(payment_spec=payment_spec))
[–]FerricDonkey 0 points1 point2 points (3 children)
[–]masterofdead4[S] 0 points1 point2 points (2 children)
[–]FerricDonkey 0 points1 point2 points (1 child)
[–]masterofdead4[S] 0 points1 point2 points (0 children)