python多线程加了join函数为什么会变慢?

查看:659
本文介绍了python多线程加了join函数为什么会变慢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

这是在廖雪峰网站上的代码;

#coding:utf-8
import time, threading
def loop():
    print 'thread %s is running...' % threading.current_thread().name
    n = 0
    while n < 10:
        n = n + 1
        print 'thread %s >>> %s' % (threading.current_thread().name, n)
        time.sleep(0.1)
    print 'thread %s ended.' % threading.current_thread().name

print 'thread %s is running...' % threading.current_thread().name
t = threading.Thread(target=loop, name='LoopThread')
t.start()
#t.join()
print 'thread %s ended.' % threading.current_thread().name

我自己也写了一个多线程的小脚本:
https://github.com/hzlRises/hzlgithub/blob/master/RankingMonitoring/monitoring.py
在我写的这个里,为什么加了join函数以后,运行比不加join要慢很多
如果join函数的意思是等待上一个线程执行完再执行下一个线程,那写多线程的意义在哪里?

解决方案

这篇关于python多线程加了join函数为什么会变慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆