Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python TypeError: '_asyncio.Future' object is not subscriptable

Good afternoon! I am making a program that needs to POST 10,000 images to the API using the Python requests library. After sending every request, I receive a response with a hash(IpfsHash), which I need to write into a dictionary of the form: "Hash": "Number". To get started, I created a simple code (using a loop and not using async) and it worked. And then I used async to speed things up. Here is the code:

import asyncio
import requests

jsonHashes = {}
responses = []

def pinToIPFS(number):
    url = 'https://api.pinata.cloud/pinning/pinFileToIPFS'
    par = {
        'pinata_api_key': 'blabla',
        'pinata_secret_api_key': 'blabla'
    }
    file = {'file': open(str(number) + '.jpg', 'rb')}

    res = requests.post(url, headers = par, files = file)
    jsonHashes[res.json()['IpfsHash']] = number
    print(res.json()['IpfsHash'] + ' = ' + str(number))


async def main():
    loop = asyncio.get_event_loop()
    futures = []
    for i in range(2):
        futures = loop.run_in_executor(None, pinToIPFS, i)
    for i in range(2):
        jsonHashes[await futures[i].json()['IpfsHash']] = i
    

loop = asyncio.get_event_loop()
loop.run_until_complete(main())


print(jsonHashes)

But, for some reason I get the error:

TypeError: '_asyncio.Future' object is not subscriptable

And immediately after that, I get a response to the requests:

QmcZR3cpeVzQ56gyWs83dRS51rtkqjyoF167paJMPrn32w = 1
QmehUERFcR6Erha6RtScDwfm1ACpZgGPrd5NNVnYWeDoH4 = 0

P.S. For simplicity, I put 2 iterations instead of 10,000.

like image 786
maxet24 Avatar asked Sep 19 '25 23:09

maxet24


2 Answers

The problematic part is here:

    for i in range(2):
        futures = loop.run_in_executor(None, pinToIPFS, i)
    for i in range(2):
        jsonHashes[await futures[i].json()['IpfsHash']] = i

loop.run_in_executor returns one asyncio.Future object, not a list of them. I do not have an idea how to rewrite your code to make it work - firstly, I'm not familiar with asyncio, but mainly, because I see no need for that second for loop at all.

like image 187
TheEagle Avatar answered Sep 21 '25 13:09

TheEagle


You have to separate the code this way in order really await for the result:

f = await futures[i]
json = f.json()['IpfsHash']
jsonHashes[json]
like image 39
Andres Avatar answered Sep 21 '25 13:09

Andres