How to do it...

We'll define a thread by passing it a number, which represents the thread number, and finally, the result will be printed out:

  1. Import the threading module by using the following Python command:
import threading
  1. In the main program, a Thread object is instantiated with a target function called my_func. Then, an argument to the function that will be included in the output message is passed:
t = threading.Thread(target=function , args=(i,))
  1. The thread does not start running until the start method is called, and the join method makes the calling thread and waits until the thread has finished the execution, as follows:
import threading

def my_func(thread_number):
return print('my_func called by thread N°
{}'.format(thread_number))


def main():
threads = []
for i in range(10):
t = threading.Thread(target=my_func, args=(i,))
threads.append(t)
t.start()
t.join()

if __name__ == "__main__":
main()
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.60.166