有很多的场景中的事情是同时进行的,比如开车的时候手和脚共同来驾驶汽车,再比如唱歌跳舞也是同时进行的
1 import time
2 3
def sing():
4 for i in range(3):
5 print("正在唱歌...%d"%i)
6 time.sleep(1)
7 8
def dance():
9 for i in range(3):
10 print("正在跳舞...%d"%i)
11 time.sleep(1)
12
13 if __name__ == '__main__':
14 sing()
15 dance()
主线程会等待子线程结束之后在结束
import threading
import time
def demo():
for i in range(5):
print('hello world')
time.sleep(1)
if __name__ == '__main__':
t = threading.Thread(target=demo)
t.start() # 启动线程 主线程会等待子线程结束之后 主线程在结束
print(1)
# 输出
hello world
1
hello world
hello world
hello world
hello world
join() 等待子线程结束之后,主线程继续执行
import threading
import time
def demo():
for i in range(5):
print('hello world')
time.sleep(1)
if __name__ == '__main__':
t = threading.Thread(target=demo)
t.start() # 启动线程 主线程会等待子线程结束之后 主线程在结束
t.join() # 等待子线程结束之后,主线程继续执行
print(1)
# 输出
hello world
hello world
hello world
hello world
hello world
1
setDaemon() 守护线程,不会等待子线程结束
import threading
import time
def demo():
for i in range(5):
print('hello world')
time.sleep(1)
if __name__ == '__main__':
t = threading.Thread(target=demo)
t.setDaemon(True) # 守护线程 不会等待子线程结束
t.start()
print(1)
# 输出
hello world
1
实例1
import threading
import time
def singe():
for i in range(3):
print('正在唱歌....')
time.sleep(1)
def dance():
for i in range(3):
print('正在跳舞....')
time.sleep(1)
def main():
t1 = threading.Thread(target=singe)
t2 = threading.Thread(target=dance)
t1.start()
t2.start()
if __name__ == '__main__':
main()
# 输出
正在唱歌....
正在跳舞....
正在唱歌....
正在跳舞....
正在唱歌....
正在跳舞....
实例2
1 import threading
2 import time
3
4 def demo():
5 # 子线程
6 print("hello girls")
7 time.sleep(1)
8
9 if __name__ == "__main__":
10 for i in range(5):
11 t = threading.Thread(target=demo)
12 t.start()
1 threading.enumerate() 查看当前线程的数量
import threading
import time
def demo1():
for i in range(5):
print('demo1--%d'%i)
time.sleep(1)
def demo2():
for i in range(10):
print('demo2--%d' % i)
time.sleep(1)
def main():
t1 = threading.Thread(target=demo1,name = 'demo1')
t2 = threading.Thread(target=demo2,name = 'demo2')
t1.start()
t2.start()
while True:
print(threading.enumerate())
if len(threading.enumerate()) <= 1:
break
time.sleep(1)
if __name__ == '__main__':
main()
当调用Thread的时候,不会创建线程。当调用Thread创建出来的实例对象的start方法的时候,才会创建线程以及开始运行这个线程。
继承Thread类创建线程
import threading
import time
class Main(threading.Thread):
def run(self):
for i in range(5):
print(i)
if __name__ == '__main__':
m = Main()
m.start()
1 import threading
2 import time
3
4 class A(threading.Thread):
5
6 def __init__(self,name):
7 super().__init__(name=name)
8
9 def run(self):
10 for i in range(5):
11 print(i)
12
13 if __name__ == "__main__":
14 t = A('test_name')
15 t.start()
在一个函数中,对全局变量进行修改的时候,是否要加global要看是否对全局变量的指向进行了修改,如果修改了指向,那么必须使用global,仅仅是修改了指向的空间中的数据,此时不用必须使用global线程是共享全局变量
import threading
import time
# 线程间是共享的全局变量
num = 100
def demo1():
global num
num += 1
print('demo1--%d'% num)
def demo2():
print('demo2--%d' % num)
def main():
t1 = threading.Thread(target = demo1)
t2 = threading.Thread(target = demo2)
t1.start()
time.sleep(1) # 保证demo1先执行
t2.start()
print('main-num=%s'% str(num))
if __name__ == '__main__':
main()
1 threading.Thread(target=test, args=(num,))
import threading
import time
num = [11,22]
def demo1(nums):
num.append(nums)
print('demo1--%s'% str(num))
def demo2():
print('demo2--%s' % str(num))
def main():
t1 = threading.Thread(target = demo1,args=(33,))
t2 = threading.Thread(target = demo2)
t1.start()
time.sleep(1) # 保证demo1先执行
t2.start()
print('main-num=%s'% str(num))
if __name__ == '__main__':
main()
# 1.有12个备选选项和2个功能按钮 确定备选选项和功能按钮的位置
# 2.点击开始会不断旋转。选中的时候背景颜色为红色,点击停止结束
import threading
import tkinter
import time
# 1.实现窗口
root = tkinter.Tk()
root.title('抽奖,试试手气')
root.minsize(300,300)
# 2 摆放按钮
btn1 = tkinter.Button(root, text='1元', bg='red')
btn1.place(x=20, y=20, width=50, height=50)
btn2 = tkinter.Button(root, text='50元', bg='white')
btn2.place(x=90, y=20, width=50, height=50)
btn3 = tkinter.Button(root, text='100元', bg='white')
btn3.place(x=160, y=20, width=50, height=50)
btn4 = tkinter.Button(root, text='1万', bg='white')
btn4.place(x=230, y=20, width=50, height=50)
btn5 = tkinter.Button(root, text='单车', bg='white')
btn5.place(x=230, y=90, width=50, height=50)
btn6 = tkinter.Button(root, text='摩托', bg='white')
btn6.place(x=230, y=160, width=50, height=50)
btn7 = tkinter.Button(root, text='汽车', bg='white')
btn7.place(x=230, y=230, width=50, height=50)
btn8 = tkinter.Button(root, text='法拉利', bg='white')
btn8.place(x=160, y=230, width=50, height=50)
btn9 = tkinter.Button(root, text='宾利', bg='white')
btn9.place(x=90, y=230, width=50, height=50)
btn10 = tkinter.Button(root, text='直升机', bg='white')
btn10.place(x=20, y=230, width=50, height=50)
btn11 = tkinter.Button(root, text='别墅', bg='white')
btn11.place(x=20, y=160, width=50, height=50)
btn12 = tkinter.Button(root, text='航母', bg='white')
btn12.place(x=20, y=90, width=50, height=50)
#2.1将所有的选项放到列表中 目的:为了操作这些选项
hero_list = [btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,btn10,btn11,btn12]
# 定义一个标记
stop_sign = False # 默认是停止
stop_id = None
# 3 定义一个函数 1.循环备选选项 2.设置选项颜色
def round():
global stop_id
i = 1
if isinstance(stop_id,int):
i = stop_id
while True:
time.sleep(0.3)
for x in hero_list:
x['bg'] = 'white'
hero_list[i]['bg'] = 'red'
i += 1
print('当前的i为:',i)
if i >= len(hero_list):
i = 0
if stop_sign == True:
stop_id = i
break
# 4 定义停止的方法
def stop():
global stop_sign
if stop_sign == True:
return
stop_sign = True
# 5 定义开始的方法
def newtask():
global stop_sign
stop_sign = False
t = threading.Thread(target=round)
t.start()
# 6.设置按钮
btn_start = tkinter.Button(root,text='开始',command=newtask)
btn_start.place(x=90,y=125,width=50,height=50)
btn_stop = tkinter.Button(root,text='停止',command=stop)
btn_stop.place(x=160,y=125,width=50,height=50)
# 显示窗口
root.mainloop()