from threading import Thread import threading from multiprocessing import Process import os def work(): import time time.sleep(3) print("thread say:",threading.current_thread().getName()) if __name__ == '__main__': # 在主进程下开启线程 t = Thread(target=work) t.start() print("getname:",threading.current_thread().getName()) print("current_thread:",threading.current_thread()) # 主线程 print("enumerate:",threading.enumerate()) # 连同主线程在内有两个运行的线程 print("active_count:",threading.active_count()) print('主线程/主进程')Outputs
macname@MacdeMacBook-Pro py % python3 cccccc.py getname: MainThread current_thread: <_MainThread(MainThread, started 4554411456)> enumerate: [<_MainThread(MainThread, started 4554411456)>, <Thread(Thread-1, started 123145514541056)>] active_count: 2 主线程/主进程 thread say: Thread-1 macname@MacdeMacBook-Pro py %