Let's say I have 5 different lists with stuff (list1, list2, list3, list4, list5). How can I make each thread take a different list as parameter? I've seen a few different ways to call threads but none of them uses different parameters
Asked
Active
Viewed 103 times
-1
-
What have you tried so far? – Shubham Sharma Apr 20 '20 at 12:19
-
Bit of terminology: You don't _call_ threads. You "start" or "launch" a thread, and the thread calls the functions that you wrote for it. – Solomon Slow Apr 20 '20 at 12:24
-
1Re, "...different ways...none of them uses different parameters." How about `threading.Thread(target=..., args=(...))` ? The ... in the `args` tuple will be supplied as positional arguments to the `target` function. You can also supply a `kwargs=...` argument. I'll leave the details of that as an exercise for the reader – Solomon Slow Apr 20 '20 at 12:25
1 Answers
-1
You can declare all lists globally and then and use them in each thread by accessing that global variable. Also other functions can make changes to it by accessing that variable.

JenilDave
- 606
- 6
- 14
-
2Using global variables is a recipe for disaster. There are better ways! – JohanL Apr 20 '20 at 12:19
-
-
You could just pass the list as an input parameter to your thread, for one thing: `threading.Thread(target=..., args=(...,...))`. – JohanL Apr 20 '20 at 12:24
-
@JohanL Yes I know this, i think he was facing problems doing this, so did I suggested that way! – JenilDave Apr 20 '20 at 12:27
-
-
I was using your way @JohanL "threading.Thread(target=..., args=(...,...)" and my script is working, but I have 2 problems. Firstly my script looks like a 10 yo wrote it cause I'm using this 10 times :D. Secondly my script crashes(or does nothing) after a while. – Deadmeat Apr 20 '20 at 12:45
-
@SolomonSlow I have worked on big projects, both of methods have proved me helpful. You never know what can be the correct way untill you try yourself! – JenilDave Apr 20 '20 at 12:46
-
@JenilDave could you please give an example of how to do the global vars and the accessing? – Deadmeat Apr 20 '20 at 12:47
-
@Deadmeat check this out https://stackoverflow.com/questions/423379/using-global-variables-in-a-function/423596#423596 – JenilDave Apr 20 '20 at 12:49
-
@JenilDave Ok, cool. Now let's say i made 2 global lists, and created 2 threads. How does the thread recognizes what list to use? I mean how do you tell "If you are thread 1 use list1, else use list2"? – Deadmeat Apr 20 '20 at 12:54