Python多线程案例

时间:2023-01-25 03:12:03
 from time import ctime,sleep
import threading
def music():
for i in range(2):
print ("I was listening to music. %s" %ctime())
sleep(4) def move():
for i in range(2):
print ("I was at the movies! %s" %ctime())
sleep(5)
def eat():
for i in range(2):
print ("I was at the eats! %s" %ctime())
sleep(10)
Treads=[]
t1 = threading.Thread(target=music)
Treads.append(t1)
t2 = threading.Thread(target=move)
Treads.append(t2)
t3 = threading.Thread(target=eat)
Treads.append(t3)
if __name__ == '__main__':
for t in Treads:
t.setDaemon(True)
t.start()
for t in Treads:
t.join()
print ("all over %s" %ctime())