0

So basically my plan for today was to create multiply profiles from json and make them run as a thread. The problem is that I have never done threading but I have read about it and it doesn't feel that complicated but as a first try and also a advice for this could be really useful for me to make something like this possible.

However what I was thinking is to have a json file looking like this:

[{
    "Name": "Baller",
    "Lastname": "cavs",
    "Email": "[email protected]"
},
{
    "Name": "Lakers",
    "Lastname": "Nation",
    "Email": "[email protected]"
}

]

which I hope is correct validated by Json at this moment.

So basically with the json file is that every profile made there should run into a thread that makes the logic into my script but at this moment I just have

  {
        "Name": "Baller",
        "Lastname": "cavs",
        "Email": "[email protected]"
    }

which only allow once and if I add [ ] to make multiple tasks, I will get a error because it doesn't know what [] are doing... but how do I need to do/think to make it run on a multiple json tasks and to make them run into a thread (I assume thread is the best to go?). What I want it to do is that make each profile into a thread. Do the same logical code in my script/code and then yes.. End when its done pretty much.

Please feel free to comment. I will also edit this by the time I get further!


EDIT:

  {
    "Profiles": {
        "profile1": {
            "Name": "Baller",
            "Lastname": "cavs",
            "Email": "[email protected]"
        },
        "profile2": {
            "Name": "Lakers",
            "Lastname": "Nation",
            "Email": "[email protected]"
        }
    }
}

I have changed my Json file into that and what code I have been trying to do is:

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

Name = config["Name"]
print(Name)

but i'm getting a error saying KeyError: 'Name'

I assume it couldn't find it at all?.. So here i am.. I think what needs to be done is maybe a loop through profiles? But then im thinking like if I do a loop then it will just run 1 by 1 and not all of the profiles at the same time?

3
  • Can you provide some code which you maybe already tried ? Commented Sep 20, 2017 at 10:04
  • Yes. I will edit now the result I have done Commented Sep 20, 2017 at 10:06
  • @YaroslavSurzhikov There you go Commented Sep 20, 2017 at 10:09

1 Answer 1

1

You get KeyError, because name is not in dictionary. Try get profile with config['Profiles']['profile1']['Name'] for example. And your first solution with list should work too if you iterate over the elements in it.

assuming your JSON looks like:

[ {profile1}, {profile2}, etc ]

Your code would be:

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

for profile in config:
    Name = profile["Name"]
    print(Name)

On edited json example:

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

for profile_cfg in config['Profiles'].values():
    Name = profile_cfg["Name"]
    print(Name)
Sign up to request clarification or add additional context in comments.

4 Comments

Oh okey. so the question is. Is it gonna run like each profile for each other? Like first the profile 1 and then profile 2? In that case. If I want them to run at the same time. I assume I need to use the thread then? @Yaroslav
But also if iam reading it correct. That means that if I have a code we say etc 300 lines. I need to make all of it in dented block to make it work as a loop, Don't I? @YaroslavSurzhikov
No, you dont, because you can wrap your code in class or function or even into module and execute it with threading/multipricessing from that loop. As for first question - if you use list of dictionaries - it will. But if you using dictionary of dictionaties - its not ( dictionaries are unordered in python )
Yeah. Currently I do not have anything at this moment with Thread and Mutiprocessing but I might think of it. Currently I tried the code you gave exemple but it didnt go profile by profile. Or that it does each profile and excute the code I have written for each profile.. If i remember right it wont notice which profile to start so it assume the last one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.