0

Let's say I have a list of strings that will be the parameters of a function.

How do I start n threads (n being the length of the list of parameters), all executing the same function, each one with a parameter from the list?

2
  • that's too broad. And note that if you want to make processing faster, threads won't help as python uses GIL (global interpreter lock). You'll have to use multiprocessing instead. Commented Mar 25, 2017 at 20:24
  • Well but, how do I do this? Commented Mar 25, 2017 at 20:32

1 Answer 1

-1
import threading single_params = ['param1', 'param2', 'param3'] threads = [] # f() will be a function that takes a single string parameter for p in single_params: threads.append(threading.Thread(target=f, args=(p)) for thread in threads: thread.start() 
Sign up to request clarification or add additional context in comments.

5 Comments

You did not mention the threads in any way
True. I assumed you knew how to start threads. If not, this should be helpful docs.python.org/2/library/threading.html#threading.Thread
Could you be more clear? I mean, I know how to start threads one by one by hand, but can I automate the process, I don't know, with a for loop?
Oh I get it now. Let me update
I updated it but consensus seems that multiprocessing is the way to go

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.