我对线程模块很陌生,但我的问题是线程似乎没有启动。我尝试使用 currentThread 函数来查看它们是否是新线程启动,但我唯一看到的是主线程。另外,我看到的每个教程都使用类或子类,例如类 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()