多线程指的是:多个线程可以共享一个cpu,cpu在每个线程上运行一会儿,让我们感觉到是多任务在运行,但其实同一时间同一核心只能运算一个东西。
python里多线程在模块threading里
他的用法如下:
import threading
import time
#用方法来写
def run(n):
print("run",n)
time.sleep(2)
t1 = threading.Thread(target=run,args=("1",))
t2 = threading.Thread(target=run,args=("2",))
t1.start()
t2.start()
#-----------------------------------------------------
#用类来写
class myThread(threading.Thread):
def __init__(self, n):
super(myThread, self).__init__()
self.n = n
def run(self):
print("runing task", self.n)
t1 = myThread("1")
t2 = myThread("2")
t1.start()
t2.start()