我对多线程模块比较陌生,但我的问题是线程似乎无法启动。我尝试使用currentThread函数来查看是否有新的线程启动,但我只能看到主线程。此外,在每个教程中,我都看到他们使用类或子类,如class t(threading.Thread)。那么我的方法是错误的还是我必须在Python 3中使用类来启动线程。
这是我写的一些脚本:
第一个:
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()
第二点:
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()