Python中实现多线程操作

# _*_ coding:utf-8 _*_
"""
file: 01.py
date: 2018-07-25 1:51 PM
author: westos-dd
desc:
线程是操作系统能够进行运算调度的最小单位(程序执行流的最小单元)
它被包含在进程之中,是进程中的实际运作单位。一个进程中可以并发多个线程
每条线程并行执行不同的任务
(线程是进程中的一个实体,是被系统独立调度和分派的基本单元)

每一个进程启动时都会最先产生一个线程,即主线程
然后主线程会再创建其他的子线程
"""
from time import ctime,sleep
def music(a):
    for i in range(2):
        print 'I was listening to %s. %s' % (a,ctime())
        sleep(1)
def movie(b):
    for i in range(2):
        print 'I was watching to %s. %s' % (b,ctime())
        sleep(5)
music('告白气球')
movie('泰坦尼克号')
print 'all over %s' %ctime()

实验结果

2线程和函数建立关系

# _*_ coding:utf-8 _*_
"""
file:2.py
date:2018-07-25 2:29 PM
author: FATRAT 
desc:
"""
from threading import Thread
def Foo(arg):
    print arg

print 'before'
# 线程和函数建立关系
t1 = Thread(target=Foo,args=(1,))
t1.start()
print 'after'

3.

# _*_ coding:utf-8 _*_
"""
file: 03.py
date: 2018-07-25 2:24 PM
author: westos-dd
desc:
    

"""
from threading import Thread
def Foo(arg):
    print arg

print 'before'
t1 = Thread(target=Foo,args=(1,))
t1.start()
print t1.getName()

t2 = Thread(target=Foo,args=(2,))
t2.start()
print t2.getName()

print 'after'

实验结果为

/usr/bin/python2.7 /home/kiosk/PycharmProjects/python/day8xia/3.py
before
1Thread-1
2Thread-2

after


Process finished with exit code 0

4.双线程操作

# _*_ coding:utf-8 _*_
"""
file:4.py
date:2018-07-25 2:34 PM
author: FATRAT 
desc:
"""
import threading
from time import ctime,sleep
def music(a):
    for i in range(2):
        print 'I was listening to %s. %s' % (a,ctime())
        sleep(1)
def movie(b):
    for i in range(2):
        print 'I was watching to %s. %s' % (b,ctime())
        sleep(5)

#music('告白气球')
#movie('泰坦尼克号')
t1 = threading.Thread(target=music,args=('告白气球',))
t1.start()
t2 = threading.Thread(target=movie,args=('泰坦尼克号',))
t2.start()
print 'all over %s' %ctime()

5.

# _*_ coding:utf-8 _*_
"""
file:5.py
date:2018-07-25 3:07 PM
author: FATRAT 
desc:
"""
from threading import Thread
import time
def Foo():
    for item in range(10):
        print item
        time.sleep(1)
print 'before'
t1 = Thread(target=Foo)
t1.start()
# 主线程到join()就不往下走了,直到子线程执行完
t1.join(5)
print 'after'

6

消费者模型

# _*_ coding:utf-8 _*_
"""
file:6.py
date:2018-07-25 3:35 PM
author: FATRAT 
desc:
"""
import threading
import Queue
import time
import random


def Producer(name, que):
    while True:
        que.put('baozi')
        print '%s:Made a baozi..=============' % name
        time.sleep(random.randrange(5))


def Consumer(name, que):
    while True:
        que.get()
        print '%s:Got a baozi..' % name
        time.sleep(random.randrange(3))


# 创建队列
q = Queue.Queue()

p1 = threading.Thread(target=Producer, args=['chef1', q])
p2 = threading.Thread(target=Producer, args=['chef2', q])
p1.start()
p2.start()

c1 = threading.Thread(target=Consumer, args=['tom', q])
c2 = threading.Thread(target=Consumer, args=['harry', q])
c1.start()
c2.start()

结果

/usr/bin/python2.7 /home/kiosk/PycharmProjects/python/day8xia/6.py
chef1:Made a baozi..=============chef2:Made a baozi..=============
 tom:Got a baozi..
tom:Got a baozi..

chef1:Made a baozi..=============harry:Got a baozi..

chef2:Made a baozi..=============tom:Got a baozi..

chef2:Made a baozi..=============
chef2:Made a baozi..=============
tom:Got a baozi..
harry:Got a baozi..
chef1:Made a baozi..=============
chef2:Made a baozi..=============
tom:Got a baozi..harry:Got a baozi..

chef1:Made a baozi..=============
tom:Got a baozi..
chef2:Made a baozi..=============harry:Got a baozi..

chef2:Made a baozi..=============
tom:Got a baozi..
chef2:Made a baozi..=============harry:Got a baozi..
chef2:Made a baozi..=============

chef2:Made a baozi..=============
chef1:Made a baozi..=============tom:Got a baozi..

chef2:Made a baozi..=============
tom:Got a baozi..
chef1:Made a baozi..=============
harry:Got a baozi..
tom:Got a baozi..
tom:Got a baozi..
chef1:Made a baozi..=============chef2:Made a baozi..=============tom:Got a baozi..

 chef2:Made a baozi..=============
 
harry:Got a baozi..
chef1:Made a baozi..=============
harry:Got a baozi..
 tom:Got a baozi..chef1:Made a baozi..=============chef2:Made a baozi..=============


tom:Got a baozi..
tom:Got a baozi..
chef2:Made a baozi..=============
chef1:Made a baozi..=============
chef1:Made a baozi..=============
tom:Got a baozi..harry:Got a baozi..chef2:Made a baozi..=============


chef2:Made a baozi..=============
tom:Got a baozi..
harry:Got a baozi..
tom:Got a baozi..
chef1:Made a baozi..=============
harry:Got a baozi..
chef2:Made a baozi..=============
harry:Got a baozi..
chef1:Made a baozi..=============tom:Got a baozi..

chef2:Made a baozi..=============
 harry:Got a baozi..
chef1:Made a baozi..=============tom:Got a baozi..

chef2:Made a baozi..=============
 chef2:Made a baozi..=============tom:Got a baozi..harry:Got a baozi..



多线程的应用

# _*_ coding:utf-8 _*_
"""
file: 07.py
date: 2018-07-25 3:42 PM
author: westos-dd
desc:
  多线程能干什么:
生产者消费者问题:(经典)
一直生产 一直消费 中间有阀值 避免供求关系不平衡

#线程安全问题,要是线程同时来,听谁的
#锁:一种数据结构 队列:先进线出 栈:先进后出

#生产者消费者的优点(为什么经典的设计模式)
	1.解耦(让程序各模块之间的关联性降到最低)
		假设生产者和消费者是两个类,如果让生产者直接调用消费者的某个方法,那么生产者对于消费者就会产生依赖(也就是耦合),
		如果将来消费者的代码发生变换,可能会影响到生产者,而如果两者都依赖于某个缓冲区,两者之间不直接依赖,
		耦合也就相应降低了
	生活中的例子:我们 邮筒 邮递员
举个例子,我们去邮局投递信件,如果不使用邮筒(也就是缓冲区),你必须得把信直接交给邮递员,有同学会说,
直接交给邮递员不是挺简单的嘛,其实不简单,你必须得认识邮递员,才能把信给他(光凭身上的制服,万一有人假冒呢???),
这就产成你和邮递员之间的依赖了(相当于生产者消费者强耦合),万一哪天邮递员换人了,
你还要重新认识一下(相当于消费者变化导致修改生产者代码),而邮筒相对来说比较固定,
你依赖它的成本就比较低(相当于和缓冲区之间的弱耦合)
	2.支持并发
		生产者消费者是两个独立的并发体,他们之间是用缓冲区作为桥梁连接,生
		产者之需要往缓冲区里丢数据,就可以继续生产下一个数据,而消费者者只需要从缓冲区里拿数据即可,
		这样就不会因为彼此速度而发生阻塞
	接着上面的例子:如果我们不使用邮筒,我们就得在邮局等邮递员,直到他回来了,我
	们才能把信给他,这期间我们啥也不能干(也就是产生阻塞),或者邮递员挨家挨户的问(产生论寻)
	3.支持忙闲不均
		如果制造数据的速度时快时慢,缓冲区的好处就体现出来了,当数据制造快的时候,
		消费者来不及处理,未处理的数据可以暂时存在缓冲区中,等生产者的速度慢下来,
		消费者再慢慢处理
	情人节信件太多了,邮递员一次处理不了,可以放在邮筒中,下次在来取

"""
# _*_ coding:utf-8 _*_
"""
file: 事件驱动.py
date: 2018-07-25 3:54 PM
author: westos-dd
desc:
    

"""
import threading
import time

def Producer():
    print 'chef:等人来买包子'
    #收到了消费者的event.set 也就是把这个flag改为了true,但是我们的包子并没有做好
    event.wait()
    #此时应该将flag的值改回去
    event.clear()
    print 'chef:someone is coming for 包子'
    print 'chef:making a 包子 for someone'
    time.sleep(5)
    # 告诉人家包子做好了
    print '你的包子好了~'
    event.set()

def Consumer():
    print 'tom:去买包子'
    # 告诉人家我来了
    event.set()
    time.sleep(2)
    print 'tom:waiting for 包子 to be ready'
    event.wait()
    print '哎呀~真好吃'

event = threading.Event()

p1 = threading.Thread(target=Producer)
c1 = threading.Thread(target=Consumer)
p1.start()
c1.start()

result

/usr/bin/python2.7 /home/kiosk/PycharmProjects/python/day8xia/7.py
chef:等人来买包子
tom:去买包子
chef:someone is coming for 包子
chef:making a 包子 for someone
tom:waiting for 包子 to be ready
你的包子好了~
哎呀~真好吃

Process finished with exit code 0
# _*_ coding:utf-8 _*_
"""
file: 异步.py
date: 2018-07-25 3:54 PM
author: westos-dd
desc:
    

"""
import threading
import time

def Producer():
    print 'chef:等人来买包子'
    # 收到了消费者的event.set 也就是把这个flag改为了true,但是我们的包子并没有做好
    event.wait()
    # 此时应该将flag的值改回去
    event.clear()
    print 'chef:someone is coming for 包子'
    print 'chef:making a 包子 for someone'
    time.sleep(5)
    # 告诉人家包子做好了
    print '你的包子好了~'
    event.set()

def Consumer():
    print 'tom:去买包子'
    # 告诉人家我来了
    event.set()
    time.sleep(2)
    print 'tom:waiting for 包子 to be ready'
    # 我在不断检测,但我已经不阻塞了
    while True:
        if event.is_set():
            print 'Thanks~'
            break
        else:
            print '怎么还没好呀~'
            # 模拟正在做自己的事情
            time.sleep(1)
event = threading.Event()

p1 = threading.Thread(target=Producer)
c1 = threading.Thread(target=Consumer)
p1.start()
c1.start()
/usr/bin/python2.7 /home/kiosk/PycharmProjects/python/day8xia/8.py
chef:等人来买包子
tom:去买包子
chef:someone is coming for 包子
chef:making a 包子 for someone
tom:waiting for 包子 to be ready
怎么还没好呀~
怎么还没好呀~
怎么还没好呀~
你的包子好了~
Thanks~

Process finished with exit code 0
# _*_ coding:utf-8 _*_
"""
file: 09.py
date: 2018-07-25 4:11 PM
author: westos-dd
desc:


"""
import threading
import time

num = 0


def run(n):
    time.sleep(1)
    global num
    lock.acquire()
    num += 1
    print '%s\n' % num
    lock.release()


lock = threading.Lock()
for i in range(4000):
    t = threading.Thread(target=run, args=(i,))
    t.start()

安全的运行

猜你喜欢

转载自blog.csdn.net/a313434458/article/details/81202218
今日推荐