0

I do have a small python script that aims to run 2 to 3 function in parallel. The scripts seems to work because, I do have the answer, however, it ends with an exception:

import threading

Variable2="V2"
Variable1="V1"
Variable12="V1"
Variable22="V2"
Variable23="V3"

def func1(Variable1,Variable2):
    print ("This is my  fisrt fucntion ")
    print ("My first variable is : %s ") % (Variable1)
    print ("My second variable is : %s ") % (Variable2)

def func2(Variable12,Variable22,Variable23):
    print ("This is my  fisrt fucntion ")
    print ("My first variable is : %s ") % (Variable12)
    print ("My second variable is : %s ") % (Variable22)
    print ("My third variable is : %s ") % (Variable23)

def runInParallel(*fns):
    proc = []
    for fn, arg in fns:
        p = threading.Thread(target=fn, args=(arg,))
        p.start()
        proc.append(p)
    for p in proc:
        p.join()


runInParallel( ( func1(Variable1,Variable2,) ), (func2(Variable12,Variable22,Variable23,) ) )

It gives me the following :

This is my  fisrt fucntion
My first variable is : V1
My second variable is : V2
This is my  fisrt fucntion
My first variable is : V1
My second variable is : V2
My third variable is : V3
Traceback (most recent call last):
File "testpara.py", line 69, in <module>
    runInParallel( ( func1(Variable1,Variable2,) ) , (func2(Variable12,Variable22,Variable23,) ) )
File "testpara.py", line 61, in runInParallel
    for fn, arg in fns:
TypeError: 'NoneType' object is not iterable

I checked here And it seems one of the parameters of the threading is note passing ( group=None, target=None, name=None, args=(), kwargs={} ).

Any clue?

edit : This seems to be different from here Because variables in that answer is the same one with an updated value.

5
  • 4
    Notice that you're not passing the functions to runInParallel: you're actually evaluating those functions (both in the main thread, so there's no concurrency here) and passing the results of the two functions to runInParallel. Since both functions return None, you get the error message you're seeing. Commented Mar 25, 2017 at 14:21
  • What am I missing ? .. I am not an expert in programming.. Commented Mar 25, 2017 at 14:23
  • Possible duplicate of Python threads not running in parallel Commented Mar 25, 2017 at 14:25
  • Specifically, you're missing two commas. You want: runInParallel( ( func1, (Variable1,Variable2,) ), (func2, (Variable12,Variable22,Variable23,) ) ) Commented Mar 25, 2017 at 14:26
  • I tried this before pasting asking my question here... Same result. Not working. Commented Mar 25, 2017 at 14:29

1 Answer 1

2

As pointed out by @Mark Dickinson in the comments, you're not calling runInParallel() properly, you're actually calling each function in the calling sequence (which results in the return value of each function, None, being passed as the arguments' values).

The fix is easy, just change one line in runInParallel(), and add some commas when calling it:

def runInParallel(*fns):
    proc = []
    for fn, arg in fns:
        p = threading.Thread(target=fn, args=arg)  # was args=(arg,)
        p.start()
        proc.append(p)
    for p in proc:
        p.join()

runInParallel((func1, (Variable1, Variable2,)),
              (func2, (Variable12, Variable22, Variable23,)))

Thanks to Mark Dickinson for correcting my correction. ;-)

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

3 Comments

This does not pass the arguments in the runInParallel function and gives me the folowwing : "TypeError: func2() takes exactly 3 arguments (1 given)" . func1 and func2 do not have the same number of arguments..
You also need to change args=(arg,) to args=arg when you create the Thread objects.
Thanks ! It was a matter of commas... I will answer my question with your corrections .

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.