import time
import threading
def sing(temp):
for i in range(5):
print("------sing------")
time.sleep(1)
a=1
def dance(temp):
for i in range(5):
print("-------dance--------")
time.sleep(1)
def main():
#创建线程
t1 = threading.Thread(target=sing,args= a)
t2 = threading.Thread(target=dance)
#执行线程
t1.start()
t2.start()
#查看线程列表
print(threading.enumerate())
#线程共享全局变量
#args传递参数
if __name__ == '__main__':
main()
自动运行
import threading
class AutoThread(threading.Thread):
def run(self):
print('call!!!')
if __name__ == '__main__':
a = AutoThread
a.start()
threading中定义了lock
#创建锁
mutex = treading.Lock()
#锁定
mutex.acquire()
#解锁
mutex.release()