线程
cpu调度的基本单位,操作系统通过给不同的线程分配时间片(CPU运行时长)来调度线程
多线程≈并发 多线程会存在同步问题
线程数不大于cpu数目时,不同线程运行在不同cpu上
1.多线程 threading.Thread用于提供线程相关的操作
import threading
import time
def show(arg):
time.sleep(1)
print 'thread:%d'%arg
if __name__ == "__main__":
for i in range(10):
t = threading.Thread(target=show, args=(i,))
t.start()
print 'main thread stop'
2.线程池threadpool.ThreadPool(过时,支持python2、3)
import threadpool
import time
def show(arg):
time.sleep(1)
print 'thread:%d'%arg
if __name__ == "__main__":
task_list = [1,2,3]
#创建线程池