4

I'm quite new to the threading module, but my problem is that threads appear to not start.I tried to use the currentThread function to see is they are new threads starting but the only thing I see is the Main thread.Also, in every tutorial I see they use classes or subclasses, like class t(threading.Thread). So is it my method that's wrong or do I have to use classes to start threads in python 3. Here is some scripts I wrote:

The first:

    import threading

    def basicThread(threadName,nr):
        print("Thread name ",threadName,", alive threads ",nr)

    for i in range(0,11):
        print(threading.activeCount())
        print(threading.currentThread())
        t = threading.Thread(target = basicThread,args = ("Thread - %s" %i,i,))
        t.start()
        t.join()

Second :

import threading

def openFile():
    try:
        file = open("haha.txt","r+")
        print("Finished in opening file : {0}".format(file))
    except IOError as e:
           print("Error : {0}".format(e))

def user(threadName,count):
    age = int(input("Enter your age : "))
    name = str(input("Enter your name : "))
    print(age,name)
    print(threadName,count)

threadList = []

thread_1 = threading.Thread(target = openFile)
thread_1.start()
thread_1.join()
thread_2 = threading.Thread(target = user,args = ("Thread - 2",threading.activeCount()))
thread_2.start()
thread_2.join()
0

2 Answers 2

5

What thread.join() does is it waits for the thread to end what it's doing. To allow other threads to start, move this line to the end of the procedure.

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

1 Comment

... and outside the for loop in thefirst example
1
  1. current_thread() returns main thread because you invoke it in the main method. The line printed from the method "basicThread" denotes the actual thread that runs that method ( which are the newly formed threads).

  2. Move the thread_1.join() to the bottom as in the previous answer

Comments

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.