5

I have an array filled with commands and I want to execute all of the commands at the same time.

So far I have:

import threading

...

def MyThread0():
    exec commandArray[0]

def MyThread1():
    exec commandArray[1]

t1 = threading.Thread(target=MyThread0(), args=[]).start()
t2 = threading.Thread(target=MyThread1(), args=[]).start()

While this may still be acceptable if there are only two threads (it seems to work, at least), it certainly is not if the length of the commandArray is unknown at runtime. How do I do this efficiently for x number of threads?

PS: It is entirely possible that all of this is junk as I'm new to multithreading. Constructive criticism is highly appreciated.

1
  • 4
    For starters, you don't want the () after the functions' names when creating the threads. That will call the function before the line is evaluated. Commented Jun 10, 2016 at 14:42

2 Answers 2

6

If I understand your problem correctly, it should be like this:

    import threading

    ...
    command_array = ...
    number_of_commands = len(command_array)
    ...

    def run_the_command(index):
        exec command_array[index]

    threads = []
    for i in range(number_of_commands):    
        t = threading.Thread(target=run_the_command, args=(i,))
        t.start()
        threads.append(t)

Note that:

  1. Pass run_it_boy instead of run_it_boy(), because you don't want to call it now, but let the threading module do it.
  2. It is encouraged to use snake_case for function/method name and variable name, CamelCase is for class name.

Alternative

In my opinion it is better to use something called thread pool.

Sign up to request clarification or add additional context in comments.

3 Comments

.start() doesn't return anything, so the assignment is useless.
@DeepSpace Thank you
NameError: name 't' is not defined :D
2

For starters, you don't want the () after the functions' names when creating the threads. That will call the function before the line is evaluated.

Secondly, .start() doesn't return anything, so your assignments to t1 and t2 are useless.

What you could do is, and is a bit more dynamic then the code you have, is something along the these line:

import threading

def func1():
    pass

def func2():
    pass

def func3():
    pass

funcs_to_run = [func1, func2, func3]  

threads = [threading.Thread(target=func, args=[]) for func in funcs_to_run]

This is assuming of course that you want each thread to execute a different function.

Then to start the threads:

 for thread in threads:
     thread.start()

2 Comments

This does not solve my problem in the slightest, I don't think you understood my problem at all. I don't want to write out these functions/threads because I don't know how many I need (e.g. you used 3, but it could also be 20).
Agreed, This does not answer the question. However, the code seems proper so not downvoting.

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.