WeInThis
WeInThis

Reputation: 547

Python - For loop and threads doesn't want to work together?

So iam playing around with threads now and I have come to the stage that the thread indeed works but I think i'm having issue with for loop. So the for loop works only inside the code but whenever I want it to run through entire whole code. Then it will just take the last object in the Json file.

The code

#Read json File
with open('config.json', 'r', encoding='UTF-8') as json_data:
    config = json.load(json_data)

threads = []
for i in range(len(config)):
    p = threading.Thread(args=(config[i]))
    threads.append(p)
    p.start()
    print()
    print(config[i])


NameUrl = config[i]["Url"]

myNote = config[i]["My-Note"]

def checkoutNames(NameUrl, nameID):

#Request & other codes - Removed to recude the code
#......
#......
    headers = {
        'Referer': '',
        'Content-Type': ''
    }
    payload = {
        "shared": {
            "challenge": {
                "email": config[i]["Email"],
                "PersonNumber": config[i]["PersonNumber"],
                "postal_code": config[i]["ZipCode"],
                "given_name": config[i]["Name"],
                "Last_name": config[i]["LastName"],
                "street_address": config[i]["Address"],
                "postal_code": config[i]["ZipCode"],
                "city": config[i]["City"],
                "country": config[i]["Country"],
                "email": config[i]["Email"],
                "phone": config[i]["Phone"],
            }

def checkoutNotes(NamesUrl, NamesPost):

#Request & other codes - Removed to recude the code
#......
#......

    headers = {
        'Accept': 'application/json, text/javascript, /; q=0.01',
        'Accept-Language': 'en-US,en;q=0.5',
        'Accept-Encoding': 'gzip, deflate, br',
        'Referer': NameUrl,
        'Connection': 'keep-alive'
    }
    payloadInfo = {
        "Information": {
            "Color": config[i]["Color"],
            "house_number": config[i]["houseNumber"],
            "year": config[i]["Year"]
      }
    }    
def wipe():
    os.system('cls' if os.name == 'nt' else 'clear')

def main():
    time.sleep(1)

    FindName(myNote)

if _name_ == '_main_':
    try: {
        main()
    }
    except KeyboardInterrupt:
        wipe()

So as you can see the code there is a thread at the beginning but I don't know the issue why it doesnt go through the whole code? maybe any see the issue?

This is the issue

    threads = []
for i in range(len(config)):
    p = threading.Thread(args=(config[i]))
    threads.append(p)
    p.start()
    print()
    print(config[i])

Upvotes: 0

Views: 68

Answers (1)

Perseus784
Perseus784

Reputation: 104

You should give a target to the thread, if not there is no point in using multithreading.

#p = threading.Thread(target=function_name, args=(config[i]))
#try something like this
def yourfucntion(config):
     #do your thing
     pass
if __name__ =='__main__':
    with open('config.json', 'r', encoding='UTF-8') as json_data:
          config = json.load(json_data)
    threads=[]
    for i,e in enum(config):
          threads.append(threading.Thread(target=yourfunction,args=(config[i] or e))

Upvotes: 1

Related Questions