单线程:
import threading
import time
def sing():
while True:
print("我在唱歌")
time.sleep(1)
def dance():
while True:
print("我在跳舞")
time.sleep(1)
if __name__=="__main__":
sing()
dance()
多线程:
import threading
import time
def sing():
while True:
print("我在唱歌")
time.sleep(1)
def dance():
while True:
print("我在跳舞")
time.sleep(1)
if __name__=="__main__":
singThread = threading.Thread(target=sing)
danceThread = threading.Thread(target=dance)
singThread.start()
danceThread.start()
import threading
import time
def sing(msg):
while True:
print(msg)
time.sleep(1)
def dance(msg):
while True:
print(msg)
time.sleep(1)
if __name__=="__main__":
singThread = threading.Thread(target=sing,args=("我在唱歌",))
danceThread = threading.Thread(target=dance,kwargs={"msg":"我在跳舞"})
singThread.start()
danceThread.start()