当前位置: 代码迷 >> 综合 >> Threading 学会多线程 (莫烦 Python 教程)笔记-1-添加线程 add thread
  详细解决方案

Threading 学会多线程 (莫烦 Python 教程)笔记-1-添加线程 add thread

热度:20   发布时间:2024-01-28 20:12:54.0

Threading 学会多线程 (莫烦 Python 教程)笔记-1-添加线程 add thread

导入threading库

import  threading
import  threading
def main():print(threading.active_count())    #打印当前已激活的线程print(threading.enumerate())       #具体的线程名字print(threading.current_thread())  #现在正在运行的线程if __name__ == '__main__':main()#out:
1
[<_MainThread(MainThread, started 6820)>]
<_MainThread(MainThread, started 6820)>

创建新的线程并执行任务

import  threadingdef thread_job():    #线程主要的工作print("This is an added Thread,number is %s" %threading.current_thread())def main():added_thread = threading.Thread(target=thread_job)    #添加线程added_thread.start()if __name__ == '__main__':main()#out:
This is an added Thread,number is <Thread(Thread-1, started 7300)>
  相关解决方案