基本方法一: class codingThread(threading.Thread): def run(self):#线程必须放在run里 xxx
class drawingThread(threading.Thread):#class类管理代码 def run(self):#线程必须放在run里 xxx
def duo_thread(): th1 = codingThread()#target不带括号,带括号为执行函数 th2 = drawingThread() th1.start() th2.start() if __name__ == '__main__': duo_thread()
基本方法二:
def coding(): the_therad = threading.current_thread()#显示程序哪个进程 print(the_therad.name) for x in range(3): print('%s正在写代码'% the_therad.name) time.sleep(1) def daring(): the_therad = threading.current_thread()#显示哪个程序在运行 for x in range(3): print('%s正在画图' % the_therad.name) time.sleep(1) def single_thread(): coding() daring() def d_thread(): t1 = threading.Thread(target=coding) t2 = threading.Thread(target=daring) t1.start() t2.start() if __name__ == '__main__': d_thread()