python开启多线程
程序员文章站
2023-12-23 12:50:34
...
多线程
import threading
import time
def music():
print("start to listen %s" % time.ctime())
time.sleep(3)
print("stop to listen %s" % time.ctime())
def game():
print("start to play game %s" % time.ctime())
time.sleep(5)
print("stop to play game %s" % time.ctime())
t1 = threading.Thread(target=music) # 创建子线程对象t1
t1.start() # 启动子线程t1
t2 = threading.Thread(target=game) # 创建子线程对象t2
t2.start() # 启动子线程t2
t2.join() # 使得子进程t2完成才开始下一个进程
print()
print("ending......") # 主线程