import threading
def mul_thread():
# 计算目前正在运行的threading
print(threading.active_count())
# 打印目前有哪些threading
print(threading.enumerate())
# 打印目前正在运行的threading
print(threading.current_thread())
if __name__ == '__main__':
mul_thread()
import threading
def thread_job():
print('目前正在运行的thread is %s'%threading.current_thread())
def main():
add_thread = threading.Thread(target = thread_job)
add_thread.start()
if __name__ == '__main__':
main()
import threading
def thread_job():
print('T1 start')
tim1 = time.time()
for i in range(1,10):
time.sleep(0.1)
# print('目前正在运行的thread is %s'%threading.current_thread())
tim2 = time.time()
runtime = tim2 - tim1
print(runtime)
print('T1 finish')
def main():
add_thread = threading.Thread(target = thread_job,name = 'T1')
add_thread.start()
print('all done!')
# all done比上面的T1 start 先出结果
if __name__ == '__main__':
main()
T1 start
all done!
1.000514030456543
T1 finish
import threading
def thread_job():
print('T1 start')
tim1 = time.time()
for i in range(1,10):
time.sleep(0.1)
# print('目前正在运行的thread is %s'%threading.current_thread())
tim2 = time.time()
runtime = tim2 - tim1
print(runtime)
print('T1 finish')
def main():
add_thread = threading.Thread(target = thread_job,name = 'T1')
add_thread.start()
add_thread.join()
print('all done!')
# 加入join之后,all done最后输出,join的含义就是在所有程序运行完之后再运行join以下的程序
if __name__ == '__main__':
main()
T1 start
0.9916887283325195
T1 finish
all done!
顺便说下,这里可以注意time.time()的使用,用来计时
多线程使用
import threading
def T1job():
print('T1 start')
tim1 = time.time()
for i in range(1,10):
time.sleep(0.1)
# print('目前正在运行的thread is %s'%threading.current_thread())
tim2 = time.time()
runtime = tim2 - tim1
print(runtime)
print('T1 finish')
def T2job():
print('T2 start')
print('T2 finish')
def main():
add_thread = threading.Thread(target = T1job,name = 'T1')
add_thread1 = threading.Thread(target = T2job,name = 'T2')
add_thread.start()
add_thread1.start()
# add_thread.join()
print('all done!')
# 加入join之后,finished最后输出,join的含义就是在所有程序运行完之后再运行join以下的程序
if __name__ == '__main__':
main()
运行的程序结果按照时间快慢来print,T2比T1 容易,所以T2比t1先结束
T1 start
T2 start
T2 finish
all done!
0.9829092025756836
T1 finish
import threading
def T1job():
print('T1 start')
tim1 = time.time()
for i in range(1,10):
time.sleep(0.1)
# print('目前正在运行的thread is %s'%threading.current_thread())
tim2 = time.time()
runtime = tim2 - tim1
print(runtime)
print('T1 finish')
def T2job():
print('T2 start')
print('T2 finish')
def main():
add_thread = threading.Thread(target = T1job,name = 'T1')
add_thread1 = threading.Thread(target = T2job,name = 'T2')
add_thread.start()
add_thread1.start()
add_thread.join()
add_thread1.join()
print('all done!')
# 加入join之后,finished最后输出,join的含义就是在所有程序运行完之后再运行join以下的程序
if __name__ == '__main__':
main()
T1 start
T2 start
T2 finish
0.9927868843078613
T1 finish
all done!
queue的学习
import threading
import time
from queue import Queue
def job(l,q):
for m in range(len(l)):
l[m] = l[m]**2
# return l 这里不能用return来做,必须要用q.put
# 把计算后的列表放入q中,等到线程都运行完毕之后然后再一次次取值
q.put(l)
def mult_threading():
q = Queue()
threads = []
data = [[1,2,3],[4,5,6],[7,8,9],[10,11,12]]
for i in range(4):
# args 参数里面的内容是元组
t = threading.Thread(target = job,args = (data[i],q))
t.start()
# 把所有线程都放入到列表中去
threads.append(t)
# 把所有线程都加上join,join完之后才能进行下一步操作
for thread in threads:
thread.join()
results = []
for _w in range(4):
# 从q中拿出值来,一次只能拿出来一个,因为传进去四次,所以要拿四次
results.append(q.get())
print(results)
if __name__ =='__main__':
mult_threading()
[[1, 4, 9], [16, 25, 36], [49, 64, 81], [100, 121, 144]]
学习内容来自B站:【【莫烦Python】Threading 学会多线程 Python】 https://www.bilibili.com/video/BV1jW411Y7Wj/?p=2&share_source=copy_web&vd_source=1c50ede1f30281705939476d277d68c4