小编给大家分享一下python3爬虫怎么统计多线程的运行,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

线程的基本统计
import threading
def main():
print(threading.active_count()) # 统计现在有多少个激活的线程
print(threading.enumerate()) # 是哪几个激活的线程:名字
print(threading.current_thread()) # 现在正在运行的线程
if __name__ == '__main__':
main()
输出结果为
5 [<_MainThread(MainThread, started 21440)>,, , , ] <_MainThread(MainThread, started 21440)>
表示有5个已经active的threading,一个是mainthread,另外是thread-4、thread-5、thead-3、savingThread;正在运行的是主线程。
拓展:增加target和传参args
def thread_job():
print("This is an added thread, numbet is %s."%threading.current_thread())
def main1():
# added_thread = threading.Thread() # 添加的线程,尚未给它工作
added_thread = threading.Thread(target=thread_job) # 添加的线程,同时给它定义一个工作,通过target传进去工作内容,
# 同时加上执行的语句 .start() 运行
added_thread.start()
if __name__ == '__main__':
main1()运行结果
This is an added thread, numbet is.
以上是“python3爬虫怎么统计多线程的运行”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!