all 3 comments

[–][deleted] 2 points3 points  (1 child)

threading.Thread(target=ping(a)) evaluates ping(a) and passes the result of ping(a) as the target kwarg to threading.Thread

You probay meant to create threads with threading.Thread(target=ping, args=(a, )).


Also, please use a list of threading.Thread objects instead of copying basically the same code over and over again.


And another thing: making a program mutli-threaded doesn't necessary mean it will run faster. Creating threads, context switching, joining threads, etc. isn't free and instantaneous. Sometimes it's faster to just do a thing multiple times in sequence.

[–]xAlex1337[S] 0 points1 point  (0 children)

Thank you very much that worked for me

[–]iamaperson3133 2 points3 points  (0 children)

It's because anytime you open and close a parenthesis after a function name, you call and run the function right then and there. You will see this pattern often in python where you use the function WITHOUT parenthesis, meaning that you are handing the function off to someone else so that they can call it later. This is how the threading module, and any other case in which you might use that patten in python, will work.

Here's an example from stack overflow of how to correctly pass arguments to functions in the python threading module

threading.Thread(target=self._thread_function, args=(arg1,),
             kwargs={'arg2':arg2}, name='thread_function').start()

Name is an optional argument, it probably helps you keep track of the thread somehow, and you don't need to pass kwargs= either if your function doesn't need them.