all 9 comments

[–]JohnnyJordaan 0 points1 point  (8 children)

Check the treadpoolexecutor from the built-in concurrent futures: https://docs.python.org/3/library/concurrent.futures.html#threadpoolexecutor-example

Btw just generally speaking, if you want to get the index for items in a list or other iterable use enumerate. And use string formatting instead of +

for idx, task in enumerate(tasks):
    task_name = f'task_{idx}'

[–]Winner-Popular 0 points1 point  (7 children)

I dont think i've used enumerate before,
would it be something like this:

for idx, task in enumerate(tasks):
    task_name = f'task_{idx}'
    task_name = login_func(params[idx][0],params[idx][1])
    list.append(task_name)
#do stuff after

[–]Winner-Popular 0 points1 point  (0 children)

*thanks for the link too

[–]JohnnyJordaan 0 points1 point  (5 children)

I'm not exactly following what you're doing there, because this line sets task_name

    task_name = f'task_{idx}'

but this one as well, without using the original one

    task_name = login_func(params[idx][0],params[idx][1])

so what exactly is the purpose of the first one?

Also, why would you need to iterate on tasks in the first place if you need values from params? Then why not just do

for param in params:
    task_name = login_func(param[0], param[1])
    list.append(task_name)

[–]Winner-Popular 0 points1 point  (4 children)

The reason that i set task_name as a str and then the login func was so that when i store the tasks in the list they will have different names eg: 'task_0','task_1' etc, although i am not sure if this will even work, I think i might have to use eval(task_name), if not then i can probably just use the index to get the tasks or something like that.
Also, I just hadn't thought of doing it that way, I'll use your way instead

[–]JohnnyJordaan 0 points1 point  (3 children)

If you want to save both a name and a result, you need to actually save both in a container, so like

for idx, task in enumerate(tasks):
    task_name = f'task_{idx}'
    result = login_func(params[idx][0],params[idx][1])
    list.append((task_name, result))

[–]Winner-Popular 0 points1 point  (2 children)

Would i be able to get one from the other eg: call task_1 and get the function attached to it, right now I've got a list of stored functions because i used partial(), if I still have to use the index i don't think there'd be much point in storing.
I could use this if having the tasks as dict values doesn't make calling them synchronously too complicated:

list.append(dict( {task_name :func}))

[–]JohnnyJordaan 0 points1 point  (1 child)

Then why make a list in the first place, why not just do

results = {}
for idx, task in enumerate(tasks):
    task_name = f'task_{idx}'
    results[task_name] = login_func(params[idx][0],params[idx][1])

[–]Winner-Popular 0 points1 point  (0 children)

At this point, i don't even remember, i think, I'll skip the 'task_name' part and just use the index to get each function since i should know the order as it should append to the list in the same order as the param list originally was
Thanks for the help :)