多线程 threading

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了多线程 threading相关的知识,希望对你有一定的参考价值。

多进程和多线程都可以执行多个任务,线程是进程的一部分。线程的特点是线程之间可以共享内存和变量,资源消耗少(不过在Unix环境中,多进程和多线程资源调度消耗差距不明显,Unix调度较快),缺点是线程之间的同步和加锁比较麻烦。

在Python中,同样可以实现多线程,有两个标准模块thread和threading,不过我们主要使用更高级的threading模块。
一、先来看一段代码

import threading
from time import ctime,sleep
def drink(func):
    for i in range(2):
        print "I was drink %s. %s" %(func,ctime())
        sleep(1)
def eat(func):
    for i in range(2):
        print "I was eat the %s! %s" %(func,ctime())
        sleep(1)
threads = []
t1 = threading.Thread(target=drink,args=(u‘牛奶‘,))
threads.append(t1)
t2 = threading.Thread(target=eat,args=(u‘面包‘,))
threads.append(t2)
if __name__ == ‘__main__‘:
    for t in threads:
        t.start()
    t.join()
    print "all over %s" %ctime()

结果:

I was drink 牛奶. Wed Jan 17 14:13:06 2018
I was eat the 面包! Wed Jan 17 14:13:06 2018
I was drink 牛奶. Wed Jan 17 14:13:07 2018
I was eat the 面包! Wed Jan 17 14:13:07 2018
all over Wed Jan 17 14:13:08 2018

start是启动线程,join是阻塞当前线程,即使得在当前线程结束时,不会退出。

Python中,默认情况下,如果不加join语句,那么主线程不会等到当前线程结束才结束,但却不会立即杀死该线程。
不加join结果:

I was drink 牛奶. Wed Jan 17 14:13:23 2018
I was eat the 面包! Wed Jan 17 14:13:23 2018 all over Wed Jan 17 14:13:23 2018
I was eat the 面包! Wed Jan 17 14:13:24 2018I was drink 牛奶. Wed Jan 17 14:13:24 2018

但如果为线程实例添加t.setDaemon(True)之后,如果不加join语句,那么当主线程结束之后,会杀死子线程。

import threading
from time import ctime,sleep

def drink(func):
    for i in range(2):
        print "I was drink %s. %s" %(func,ctime())
        sleep(1)

def eat(func):
    for i in range(2):
        print "I was eat the %s! %s" %(func,ctime())
        sleep(1)

threads = []

t1 = threading.Thread(target=drink,args=(u‘牛奶‘,))
threads.append(t1)
t2 = threading.Thread(target=eat,args=(u‘面包‘,))
threads.append(t2)

if __name__ == ‘__main__‘:
    for t in threads:
        t.setDaemon(True)
        t.start()
    print "all over %s" %ctime()

不加join结果:

I was drink 牛奶. Wed Jan 17 14:10:43 2018
all over Wed Jan 17 14:10:43 2018I was eat the 面包! Wed Jan 17 14:10:43 2018

如果加上join,并设置等待时间,就会等待线程一段时间再退出。

import threading
from time import ctime,sleep

def drink(func):
    for i in range(2):
        print "I was drink %s. %s" %(func,ctime())
        sleep(1)

def eat(func):
    for i in range(2):
        print "I was eat the %s! %s" %(func,ctime())
        sleep(1)

threads = []

t1 = threading.Thread(target=drink,args=(u‘牛奶‘,))
threads.append(t1)
t2 = threading.Thread(target=eat,args=(u‘面包‘,))
threads.append(t2)

if __name__ == ‘__main__‘:
    for t in threads:
        t.setDaemon(True)
        t.start()
    t.join(0.1)
    print "all over %s" %ctime()

这里设置为0.1秒结果如下:

I was drink 牛奶. Wed Jan 17 14:11:08 2018
I was eat the 面包! Wed Jan 17 14:11:08 2018
all over Wed Jan 17 14:11:08 2018

#注释:
join()方法的位置是在for循环外的,也就是说必须等待for循环里的两个进程都结束后,才去执行主进程。

以上是关于多线程 threading的主要内容,如果未能解决你的问题,请参考以下文章

线程同步-使用ReaderWriterLockSlim类

python threading超线程使用简单范例的代码

python的多线程多进程代码示例

C# Thread开启线程的几种方式

java 如何编写多线程的代码

多线程Thread类