0

I have a function which receives 2 arguments. This function actually interfaces a certain communication devices and writes several lines in its CLI.

I need to do this same CLI writing concurrently on 4 devices.

by using the following code (only an example don't refer to the contents of func1) I know how to run a thread that performs this function. But, I don't know how to start 4 threads.

import _thread

def func1(arg1, arg2):
   print("Write to CLI")


_thread.start_new_thread(func1, (DUT1_CLI, '0'))
Alfe
  • 56,346
  • 20
  • 107
  • 159
Moshe S.
  • 2,834
  • 3
  • 19
  • 31
  • You can just write on the next line new invoke of the thread, example: `_thread.start_new_thread(func1, (DUT1_CLI, '0')) _thread.start_new_thread(func1, (DUT2_CLI, '0'))` The differenciator will be – Rafał Czabaj Sep 19 '17 at 09:49
  • In python, if any name starts with an underscore you should avoid using it (in this case, importing the module). Have a look at this question for more details: https://stackoverflow.com/questions/1301346/what-is-the-meaning-of-a-single-and-a-double-underscore-before-an-object-name – GPhilo Sep 19 '17 at 09:50
  • Consider changing `_thread` for [`threading`](https://docs.python.org/2/library/threading.html) – GPhilo Sep 19 '17 at 09:53
  • I think that I need this low level threading capabilities of _thread. The difference between these two are just the ability to better manage the threads with threading. Correct me if I'm wrong – Moshe S. Sep 19 '17 at 10:01
  • I never needed `_thread`. What low-level capabilities of it do you need in your project? – Alfe Sep 19 '17 at 10:04
  • I just need a simple concurrent execution of a certain function. What other options do I have that would keep my implementation simple – Moshe S. Sep 19 '17 at 10:06

1 Answers1

1

Just repeat your last line four times.

Or use a loop:

for _ in range(4):
  _thread.start_new_thread(func1, (DUT1_CLI, '0'))

Each execution of the line will start a new thread which will also start running in parallel in that moment. So your four threads might be started rather quickly and then produce output. Depending on buffering, your terminal type etc. this might even lead to intermingled output (two "Hello World" outputs might become "HelHello Wlo Wororldld" in harsh cases).

Alfe
  • 56,346
  • 20
  • 107
  • 159
  • Great, works fine. If I understand it right all the threads are automatically closed and don't hold resources anymore once they finished their tasks. Right ? – Moshe S. Sep 19 '17 at 09:59
  • This is technically a new question, but yes. If a thread has nothing more to do it terminates. The father thread should `join()` this thread, though. Otherwise some internal data structures linger and wait for this joining step to happen. If your whole process terminates, all threads are cleaned up anyway. – Alfe Sep 19 '17 at 10:01
  • Can I use the same thread to run two functions (one after the other) ? Or, do I need to gather these tasks under the same function in order to accomplish this task ? – Moshe S. Sep 19 '17 at 10:52
  • 1
    A thread runs one function. However, this function can call as many other functions as it likes. So, effectively, yes, you can use a thread two perform to different functions one after the other. – Alfe Sep 19 '17 at 10:58